Wagmare
Wagmare

Reputation: 1416

qt dialog to show location to save the created text file

i have generated a .c and .h file from the entries in my window. now i want to show a dialog where the user can select a path to save this two files to a folder. QFileDialog::getSaveFileName will help to get the path but i dont want user to change the fileName and i want to save two files using the same dialog.

Upvotes: 0

Views: 2239

Answers (1)

Jeet
Jeet

Reputation: 1046

One of the way to make the defined name unchangeable is by making the text edit read only (in the Qfiledialog). Below is sample code will do the input box read-only. Comment in the code will explain in detail

//Show the file save dialog in a button click
    void MainWindow::on_cmdShowSave_clicked()
    {
        //QFileDialog object
        QFileDialog objFlDlg(this);
        //Set the file dialog optin to show the directory only
        objFlDlg.setOption(QFileDialog::ShowDirsOnly, true);
        //Show the file dialog as a save file dialog
        objFlDlg.setAcceptMode(QFileDialog::AcceptSave);
        //The constant file name
        objFlDlg.selectFile("The_Name_You_Want");
        //Make the file dialog file name read only
        //The file name entering widget is a QLineEdit
        //So find the widget in the QFileDialog
        QList<QLineEdit *> lst =objFlDlg.findChildren<QLineEdit *>();
        qDebug() << lst.count();
        if(lst.count()==1){
            lst.at(0)->setReadOnly(true);
        }else{
            //Need to be handled if more than one QLineEdit found
        }
        //Show the file dialog
        //If save button clicked
        if(objFlDlg.exec()){
             qDebug() << objFlDlg.selectedFiles().at(0)+".c";
             qDebug() << objFlDlg.selectedFiles().at(0)+".h";
        }
    }

Output:
"/home/linuxFedora/Jeet/Documents/The_Name_You_Want.c"
"/home/linuxFedora/Jeet/Documents/The_Name_You_Want.h"

Upvotes: 1

Related Questions