选择文件
1 public: 2 //EXCEL路径 3 QString filepath; 4 5 private slots: 6 int m_openFileButton(bool checked); 7 8 9 //写在ui.setupUi(this);下面 10 connect(ui.openFileButton, SIGNAL(clicked(bool)), this, SLOT(m_openFileButton(bool))); 11 12 13 14 int TestAandB::m_openFileButton(bool checked) 15 { 16 //定义文件对话框类 17 QFileDialog *fileDialog = new QFileDialog(this); 18 //定义文件对话框标题 19 fileDialog->setWindowTitle(GBK::ToUnicode("选择EXCEL")); 20 //设置默认文件路径 21 fileDialog->setDirectory("."); 22 //设置文件过滤器 23 fileDialog->setNameFilter(tr("EXCEL(*.xlsx)")); 24 //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles 25 fileDialog->setFileMode(QFileDialog::ExistingFiles); 26 //设置视图模式 27 fileDialog->setViewMode(QFileDialog::Detail); 28 //打印所有选择的文件的路径 29 QStringList fileNames; 30 if (fileDialog->exec()) 31 { 32 fileNames = fileDialog->selectedFiles(); 33 if (fileNames.length() > 0) 34 { 35 ui.showFilePath->setText(fileNames[0]); 36 filepath = fileNames[0]; 37 } 38 } 39 return 0; 40 }
选择文件getOpenFileName
1 int TestAandB::m_openFileButton(bool checked) 2 { 3 // 选择要打开的文件 4 QString filepath = QFileDialog::getOpenFileName(this, GBK::ToUnicode("选择文件"), GBK::ToUnicode("*.txt"), GBK::ToUnicode("文本文档(*.txt)")); 5 if (filepath.length() > 0) 6 { 7 ui.showFilePath->setText(filepath); 8 } 9 return 0; 10 }
保存文件getSaveFileName
1 int TestAandB::m_openFileButton(bool checked)
2 {
3 // 选择保存路径
4 QString filepath = QFileDialog::getSaveFileName(this,GBK::ToUnicode("保存"),GBK::ToUnicode("*.txt"),GBK::ToUnicode("文本文档(*.txt)"));
5 if (filepath.length() > 0)
6 {
7 ui.showFilePath->setText(filepath);
8 }
9 return 0;
10 }
选择文件夹getExistingDirectory
1 int TestAandB::m_openFileButton(bool checked) 2 { 3 //文件夹路径 4 QString srcDirPath = QFileDialog::getExistingDirectory(this, GBK::ToUnicode("选择文件夹"),"/"); 5 if (srcDirPath.length() > 0) 6 { 7 ui.showFilePath->setText(srcDirPath); 8 } 9 return 0; 10 }
...