qt对话框显示保存创建的文本文件的位置

时间:2016-02-03 06:44:44

标签: qt qdialog qfiledialog

我已从窗口中的条目生成.c和.h文件。现在我想显示一个对话框,用户可以在其中选择将这两个文件保存到文件夹的路径。 QFileDialog :: getSaveFileName将有助于获取路径,但我不希望用户更改fileName,我想使用相同的对话框保存两个文件。

1 个答案:

答案 0 :(得分:1)

使定义的名称不可更改的一种方法是使文本编辑只读(在Qfiledialog中)。下面是示例代码,将输入框设置为只读。代码中的注释将详细解释

//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";
        }
    }

输出:
&#34; /home/linuxFedora/Jeet/Documents/The_Name_You_Want.c"
&#34; /home/linuxFedora/Jeet/Documents/The_Name_You_Want.h"

相关问题