目录
打开一个文件:QFileDialog::getOpenFileName()
打开多个文件:QFileDialog::getOpenFileNames()
选择保存一个文件: QFileDialog::getSaveFileName()
选择已有的目录:QFileDialog::getExistingDirectory()
一、文件对话框 QFileDialog
常用静态函数 | 函数解释 |
QString getOpenFileName() | 选择打开一个文件 |
QStringList getOpenFileNames() | 选择打开多个文件 |
QString getSaveFileName() | 选择保存一个文件 |
QString getExistingDirectory() | 选择一个己有的目录 |
QUrl getOpenFileUrl() | 选择打幵一个文件,可选择远程网络文件 |
打开一个文件:QFileDialog::getOpenFileName()
QString fileName; fileName = QFileDialog::getSaveFileName(this,QString("选择一个文件"), curPath, tr("xls file (*.xls *.xlsx)")); if(!fileName.isEmpty()) { }
getOpenFileName() 函数需要传递 3 个字符串类型参数,分别如下:
- 对话框标题,这里设置为"选择一个文件"。
- 初始化目录,打开对话框时的初始目录,这里用QDinxurrentPath()获取应用程序当前目录。 QString curPath=QDir::currentPath();//获取系统当前目录
- 文件过滤器,设置选择不同后缀的文件,可以设置多组文件 例如:tr("xls file (*.xls *.xlsx)")
打开多个文件:QFileDialog::getOpenFileNames()
参数与getOpenFileName相同
QString curPath=QDir::currentPath();//获取系统当前目录 QString title="选择多个文件"; //对话框标题 QString filter="文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*.*)"; //文件过滤器 QStringList fileList=QFileDialog::getOpenFileNames(this,title,curPath,filter); for (int i=0; i<fileList.count();i++) { ui->textEdit->appendPlainText(fileList.at(i)); }
选择保存一个文件: QFileDialog::getSaveFileName()
参数与getOpenFileName相同。
注意:在调用 getSaveFileName() 函数时,若选择的是一个己经存在的文件,会提示是否覆盖原有的文件。如果提示覆盖,会返回为选择的文件,但是并不会对文件进行实质操作,对文件的删除操作需要在选择文件之后自己编码实现
QString curPath=QCoreApplication::applicationDirPath();//获取应用程序路径 QString title="保存文件"; //对话框标题 QString filter="文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*.*)"; //文件过滤器 QString file=QFileDialog::getSaveFileName(this,title,curPath,filter); if (!aFileName.isEmpty()) ui->textEdit->appendPlainText(file);
选择已有的目录:QFileDialog::getExistingDirectory()
QString curPath = QCoreApplication::applicationDirPath();//获取应用程序路径
QString title = "选择一个目录"; //对话框标题
QString file = QFileDialog::getExistingDirectory(this, title, curPath, QFileDialog::ShowDirsOnly);
if (!aFileName.isEmpty())
ui->textEdit->appendPlainText(file);
二、消息对话框 QMessageBox
常用静态函数 | 函数解释 |
warning() | 警告,错误信息提示 |
question() | 询问是否确认 |
information() | 信息提示 |
critical() | 错误信息提示 |
about() | 设置自定义信息提示框 |
warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton) :
//创建一个警告信息窗口,tr函数为译本函数,即译码后面的text内容 QMessageBox::warning(this,tr("警告"),tr("发送内容不能为空"),QMessageBox::Ok);
参数解释:
parent 是对话框的父窗口,指定父窗口之后,打开对话框时,对话框将自动显示在父窗口的上方中间位置;
title 是对话框标题字符串;text 是对话框需要显7K的信息字符串;
buttons 是对话框提供的按钮,缺省只有一个 OK 按钮;
defaultButton 是缺省选择的按钮,缺省表示没有选择;
几种对话框创建:
//Information void Dialog::on_btnInformation_clicked() { QString title="information消息框"; QString strInfo="文件已经打开,字体大小已设置"; QMessageBox::information(this, title, strInfo, QMessageBox::Ok,QMessageBox::NoButton); } //Warning void Dialog::on_btnWarning_clicked() { QString title="warning 消息框"; QString strInfo="文件内容已经被修改"; QMessageBox::warning(this, title, strInfo); } //Critical void Dialog::on_btnCritical_clicked() { QString title="critical消息框"; QString strInfo="有不明程序访问网络"; QMessageBox::critical(this, title, strInfo); } //About void Dialog::on_btnAbout_clicked() { QString title="about消息框"; QString strInfo="我开发的数据查看软件 V1.0 \n 保留所有版权"; QMessageBox::about(this, title, strInfo); }
确认选择对话框
QMessageBox::question(QWidget *parent, const QString &title, const QString &text,
StandardButtons buttons = StandardButtons( Yes | No ),
StandardButton defaultButton = NoButton)
此函数用于打开一个选择对话框,提示信息,并提供 Yes、No、OK、Cancel 等按钮,用户单击某个按钮返回选择。
QString title="Question消息框"; QString strInfo="文件已被修改,是否保存修改?"; QMessageBox::StandardButton defaultBtn=QMessageBox::NoButton; //缺省按钮 QMessageBox::StandardButton result;//返回选择的按钮 result=QMessageBox::question(this, title, strInfo, QMessageBox::Yes |QMessageBox::No |QMessageBox::Cancel,defaultBtn); if (result==QMessageBox::Yes) ui->plainTextEdit->appendPlainText("Question消息框: Yes 被选择"); else if(result==QMessageBox::No) ui->plainTextEdit->appendPlainText("Question消息框: No 被选择"); else if(result==QMessageBox::Cancel) ui->plainTextEdit->appendPlainText("Question消息框: Cancel 被选择"); else ui->plainTextEdit->appendPlainText("Question消息框: 无选择");