代码之家  ›  专栏  ›  技术社区  ›  Wagmare

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

  •  0
  • Wagmare  · 技术社区  · 9 年前

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

    1 回复  |  直到 9 年前
        1
  •  1
  •   Jeet    9 年前

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

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

    输出:
    “/home/linuxFedora/Jeet/Documents/The_Name_You_Want.c”
    “/home/linuxFedora/Jeet/Documents/The_Name_You_Want.h”