简体   繁体   中英

QFileDialog: add suffix after selecting file

I need to add suffix to selected filename in QFileDialog with QFileDialog::AcceptSave accept mode. For example, after selecting "1.txt" file in QFileDialog edit should be select "1_suffix.txt". It should be added before file accepting, because I need the user to have the ability to change the filename before applying file.

code:

m_dialog.setAcceptMode(QFileDialog::AcceptSave);
m_dialog.setWindowModality(Qt::WindowModal);
m_dialog.setFileMode(QFileDialog::AnyFile);
m_dialog.setDefaultSuffix("_suffix");
if(m_dialog.exec() == QFileDialog::Accept)
{
    setPath(m_dialog.selectedFiles()[0]);
}

Usually, a QFileDialog is displaying the platform file dialog. To get the behavior you want, you'd need to use platform-specific mechanisms; Qt doesn't implement such functionality.

If you're using the non-native file dialog, you could inspect its structure to find the widget(s) you're after, filter relevant events on them, and inject the behavior you need.

Try extending QFileDialog and subscribe to QFileDialog signals

  void fileSelected(QString file)
  void currentChanged(QString path)

It can be a start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM