使定义的名称不可更改的方法之一是使文本编辑为只读(在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”