1、获取文件路径对话框
file_name = QFileDialog::getOpenFileName(self,"open file dialog","C:\Users\Administrator\Desktop","Txt files(*.txt)") ##"open file Dialog "为文件对话框的标题,第三个是打开的默认路径,第四个是文件类型过滤器
2、保存文件对话框
file_path = QFileDialog::getSaveFileName(self,"save file","C:\Users\Administrator\Desktop" ,"xj3dp files (*.xj3dp);;all files(*.*)") # file_path即为文件即将保存的绝对路径。形参中的第二个为对话框标题,第三个为打开后的默认给路径,第四个为文件类型过滤器
保存文件:
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), "", tr("Curve TagName Files (*.TXT)")); if (!fileName.isEmpty()) { //一些处理工作,写数据到文件中
QList<QwtPlotCurve*>& plotCurves = m_plot->getPlotCurves(); QFile file(fileName); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) QMessageBox::critical(this, tr("Error"), tr("Failed to open file \"%1\" for save!").arg(fileName), QMessageBox::Ok); //write file
QTextStream wr(&file); for(int i=0; i<plotCurves.count(); ++i) { wr <<plotCurves[i]->title().text() <<"\n"; } file.close(); } else
return;
3、选择文件夹对话框
dir_path=QFileDialog::getExistingDirectory(self,"choose directory","C:\Users\Administrator\Desktop") # dir_path即为选择的文件夹的绝对路径,第二形参为对话框标题,第三个为对话框打开后默认的路径。 以上返回的都是QString类型的对象,若想不出现编码问题,建议用如下语句将QString转换为python的string对象 str=unicode(your_path.toUtf8(), ‘utf-8‘, ‘ignore‘)
根据指定文件夹找对应文件:
QString fileName(tr("ok.txt")) ; QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); QDir d; d.mkpath(dir);//可以不用,因为路径已经有了,就不用mk了
QFile file(dir+"/"+fileName); file.open(QFile::WriteOnly);
另存为:
QString path; path = QDir::currentPath() + "/CscanData/Cscan.bmp"; QString filename = QFileDialog::getSaveFileName(this, tr("Save As"), path, tr("Image Files (*.bmp)")); if(!filename.isNull()) { //一些处理工作,复制
QFile::copy(path+ filename); } else
return;