QT5入门之17 - 文件选择对话框
2015-10-27 14:27
405 查看
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), NULL, tr("txtFile (*.* *.txt)")); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; }
fileName 返回选择的文件,下面的使用QFile打开文件(只读方式)。
文件保存对话框类似。
void MainWindow::openFile() { QString path = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("Text Files(*.txt)")); if(!path.isEmpty()) { QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Read File"), tr("Cannot open file:\n%1").arg(path)); return; } QTextStream in(&file); textEdit->setText(in.readAll()); file.close(); } else { QMessageBox::warning(this, tr("Path"), tr("You did not select any file.")); } } void MainWindow::saveFile() { QString path = QFileDialog::getSaveFileName(this, tr("Open File"), ".", tr("Text Files(*.txt)")); if(!path.isEmpty()) { QFile file(path); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Write File"), tr("Cannot open file:\n%1").arg(path)); return; } QTextStream out(&file); out << textEdit->toPlainText(); file.close(); } else { QMessageBox::warning(this, tr("Path"), tr("You did not select any file.")); } }
内置对话框:
QColorDialog:选择颜色;
QFileDialog:选择文件或者目录;
QFontDialog:选择字体;
QInputDialog:允许用户输入一个值,并将其值返回;
QMessageBox:模态对话框,用于显示信息、询问问题等;
QPageSetupDialog:为打印机提供纸张相关的选项;
QPrintDialog:打印机配置;
QPrintPreviewDialog:打印预览;
QProgressDialog:显示操作过程。
相关文章推荐
- win7 32bit 用qmake编译qt程序
- QT5入门之16 - 设置按钮提示
- QT5入门之15 - qr函数
- qtp传参的注意要点,及小技巧
- qt中某个类的头文件无法找到
- qt环境变量配置
- Qt QLabel::setBuddy函数
- Qt使用教程之创建一个基于Qt部件的应用程序(一)
- Qt concurrent run()函数使用
- Qt之无边框自定义最小化、关闭按钮并实现窗口移动
- Qt::WA_StaticContents的作用
- QT程序Release后无法连接数据库的问题
- qt5和opencv3在linux上安装配置
- There's no Qt version assigned to this project for platform x64. Please use the 'change Qt version
- Qt中 QGLWidget碰到no such file or directory的解决办法
- Qt之四方分割器QuadSplitter
- Qt5+VS2013兼容XP方法
- 学习Qt之简单计算器
- Qt添加界面文件的背景图片
- VS2012 QT程序打包部署详解