Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Ask for confirmation in QFileDialog



  • Hello,

    i am using a QFileDialog to load files and create a TreeView out of the files data. But if a new one should be loaded i want the dialog to alert the user that the current TreeView will be deleted, so he has to confirm the loading. As i see in the docs, the QFileDialog::DontConfirmOverwrite is set to default and the cornfirmation is requested. But i dont get access to the dialog and does not show up. Anyone has a hint how i could do that?

    I use the code below to call the QFileDialog:

    QString l_filePath = QFileDialog::getIpenFileName(this,tr("Load Directory"),QDir::homePath(),("*.db"));

    then i pass this filePath to a regular open function.


  • Lifetime Qt Champion

    @meikelneit You can pass Options to the dialog: https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName. It's the last parameter.


  • Moderators

    @meikelneit said in Ask for confirmation in QFileDialog:

    QFileDialog::DontConfirmOverwrite

    That confirmation only makes sense when you are trying to save a file. It won't show when you open it.

    If you need a custom behaviour like that, use custom QFileDialog, not one of the convenience functions.



  • Ok thank you.
    Do i understand it right when i say, ill have to use my own QDialog *l_dialog = new QDialog;
    and then use a slot, which i code myself and it reacts on the QDialog::Accepted() Signal?


  • Lifetime Qt Champion

    @meikelneit
    Hi
    as already mentioned, the Dialog can warn about overwriting files but it wont when loading them, so you could just do

    QString l_filePath = QFileDialog::getIpenFileName(this,tr("Load Directory"),QDir::homePath(),("*.db"));
    if (!filepath.empty)
    QMessageBox::warning(qApp->activeWindow(), "Replace", "Data will be lost. Continue ? ", QMessageBox::Yes|QMessageBox::No);
    

    before actually removing old TreeView


  • Moderators

    @meikelneit said in Ask for confirmation in QFileDialog:

    Ok thank you.
    Do i understand it right when i say, ill have to use my own QDialog *l_dialog = new QDialog;
    and then use a slot, which i code myself and it reacts on the QDialog::Accepted() Signal?

    Yes. But you can create a QFileDialog - it will be much easier than creating custom implementation in "bare" QDialog.



  • I have a Problem now and i dont get the Point i am missing.

    I tryed two Solutions:

    QMessageBox l_Warning;
    l_Warning.setText("Beim importieren der Datei gehen die aktuellen Daten verloren.");
    l_Warning.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    l_Warning.setWindowTitle("Warnung");
    QPushButton *l_laden = l_Warning.addButton((tr("Laden"),QMessageBox::Yes));
    l_Warning.exec();

    if(l_Warning.buttonClicked() == l_laden) here appears an Error: Cant convert QPushButton* to QAbstractButton*
    {

    }

    QMessageBox l_Warning;
    l_Warning.setText("Beim importieren der Datei gehen die aktuellen Daten verloren.");
    l_Warning.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    l_Warning.setWindowTitle("Warnung");
    QAbstractButton *l_laden = l_Warning.addButton((tr("Laden"),QMessageBox::Yes)); here appears an Error: Cant convert QAbstractButton* to QPushButton*
    l_Warning.exec();

    if(l_Warning.buttonClicked() == l_laden)
    {

    }

    What am is missing?

    PS: I guess its the Action role QMessageBox::Yes, but what else could i set there, i dont see flags in the doc for Abstract Buttons.


  • Lifetime Qt Champion

    @meikelneit Shouldn't it be clickedButton()?
    If it then still complains then do the cast:

    if(l_Warning.buttonClicked() == reinterpret_cast<QAbstractButton*>(l_laden))
    


  • Ok thank you, that works. But why do i needthe cast there, in the first step i just copied my Code from the docs, ist exactly like that.
    https://doc.qt.io/qt-5/qmessagebox.html see QAbstractButton.


  • Lifetime Qt Champion

    @meikelneit What compiler do you use?



  • I use the Compiler MSVC15


Log in to reply