2016-02-03 37 views
0

我從窗口中的條目生成了一個.c和.h文件。現在我想顯示一個對話框,用戶可以選擇一個路徑將這兩個文件保存到文件夾。 QFileDialog :: getSaveFileName將有助於獲取路徑,但我不希望用戶更改文件名,我想使用同一個對話框保存兩個文件。qt對話框顯示保存創建的文本文件的位置

+1

使用'QFileDialog :: getExistingDirectory'選取目錄,然後將文件保存到其中。 – hank

回答

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

輸出:
「/home/linuxFedora/Jeet/Documents/The_Name_You_Want.c」
「/home/linuxFedora/Jeet/Documents/The_Name_You_Want.h」

相關問題