Qt——文件对话框
教程:https://www.devbean.net/2012/09/qt-study-road-2-file-dialog/
代码如下:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> class QTextEdit; class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); private slots:
void openFile();
void saveFile(); private:
QAction *openAction;
QAction *saveAction; QTextEdit *textEdit;
}; #endif // MAINWINDOW_H
//mainwindow.cpp
#include <QtGui>
#include <QtWidgets>
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
openAction = new QAction(QIcon(":/images/file-open"), tr("&Open..."), this);
openAction->setShortcuts(QKeySequence::Open);
openAction->setStatusTip(tr("Open an existing file"));
connect(openAction, &QAction::triggered, this, &MainWindow::openFile); saveAction = new QAction(QIcon(":/images/file-save"), tr("&Save..."), this);
saveAction->setShortcuts(QKeySequence::Save);
saveAction->setStatusTip(tr("Save a new file"));
connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile); QMenu *file = menuBar()->addMenu(tr("&File"));
file->addAction(openAction);
file->addAction(saveAction); QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);
toolBar->addAction(saveAction); statusBar() ; textEdit = new QTextEdit(this);
setCentralWidget(textEdit);
} MainWindow::~MainWindow()
{
} 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("Save 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."));
}
}
函数说明:
QString getOpenFileName(QWidget * parent = , //父窗口
const QString & caption = QString(), //对话框标题
const QString & dir = QString(), //打开的默认目录
const QString & filter = QString(), //过滤器
QString * selectedFilter = , //默认选择的过滤器
Options options = ) //对话框的一些参数设定,比如只显示文件夹等等,它的取值是enum
运行截图:

Qt——文件对话框的更多相关文章
- QT 文件对话框(QFileDialog)
1.选择文件(上传.打开...) QString QFileDialog::getOpenFileName( QWidget *parent = , //parent,用于指定父组件.注意,很多Qt组 ...
- QT学习 之 对话框 (四) 字体对话框、消息对话框、文件对话框、进程对话框(超详细中文注释)
QMessageBox类: 含有Question消息框.Information消息框.Warning消息框和Critical消息框等 通常有两种方式可以来创建标准消息对话框: 一种是采用“基于属性”的 ...
- QT 打开文件对话框汇总
Qstring fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ", tr( ...
- Qt打开文件对话框
项目中需要打开文件对话框,就查了一下,不得不说Qt的帮助文档做的真好,非常详细.要实现这个功能有两种方式,使用QFileDialog的静态方法,实例化QFileDialog对象. 基本算是照抄帮助文档 ...
- Qt 学习之路 2(17):文件对话框
Home / Qt 学习之路 2 / Qt 学习之路 2(17):文件对话框 Qt 学习之路 2(17):文件对话框 豆子 2012年9月24日 Qt 学习之路 2 85条评论 在前面的章节中 ...
- QT+ 使用标准对话框+关于对话框+问题对话框+文件对话框
#include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAct ...
- QT设计UI:QT模式对话框打开文件
使用QT模式对话框,并使显示框 为背景色: 方法使用了QCheckBox *native; #include <QCheckBox> 初始化函数代码: //设置默认打开图像位置 nat ...
- 19.QT对话框(文件对话框,颜色对话框,字体框,自定义对话框)
文件对话框 #include<QFileDialog> //文件对话框 void Dialog::on_pushButton_clicked() { //定义显示文件的类型 窗口标题 可供 ...
- QT 文件操作
QT提供了QFile类用于文件读写 QFile可以读写文本文件,也可以读写二进制文件 #include "widget.h" #include <QGridLayout> ...
随机推荐
- burn android images with fastboot
在qualcomm平台,使用fastboot烧写系统镜像.烧写方法记录于此. Burn emmc_appsboot.mbn adb reboot bootloader # 重启到bootloader ...
- 第三百五十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解
第三百五十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解 信号一般使用信号分发器dispatcher.connect(),来设置信号,和信号触发函数,当捕获到信号时执行 ...
- Oracle RAC集群资料收集
RAC优势和劣势 http://tech.it168.com/a2012/0814/1384/000001384756_all.shtml 双机热备与RAC并行模式对比 WAS数据源设置 http:/ ...
- Shell脚本中的export
shell 与 export命令 用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令 或声明变量,也可以创建并运行shell脚本程序.运行shell脚 ...
- PolymiRTS 数据库- miRNA SNP数据库
背景: miRNA通过和mRNA的3'UTR区结合,导致mRNA讲解或者抑制mRNA翻译,从而实现转录后调控的作用: 如果在miRNA和 mRNA的结合区域,发生了snp,就可能会影响miRNA和mR ...
- fiddler 工具使用配置
前言: 之前为了手动测试项目组之间提供的接口,确定到底是哪一个接口出了问题.一般情况下,我们都直接采用了 Google 浏览器上,F12 后,Network 找到想要的 URL,然后,直接在浏览器上访 ...
- 局域网 ping
时间:2013-01-15 11:07来源:网络整理 作者:秩名点击:2271次 |我要投稿 一般情况下,如果我们使用Ping命令来查找网络问题所在或检验网络运行情况,我们需要使用许多Ping命令,如 ...
- UNIX环境编程学习笔记(19)——进程管理之fork 函数的深入学习
lienhua342014-10-07 在“进程控制三部曲”中,我们学习到了 fork 是三部曲的第一部,用于创建一个新进程.但是关于 fork 的更深入的一些的东西我们还没有涉及到,例如,fork ...
- node配置自动监测文件改变不重启
方法一: nodemon npm install -g nodemon nodemon ./bin/www 或者在npm start命令里把node改为nodemon 方法二:supervisor n ...
- MongoDB:通过mongodump【时间一致性】备份,快速创建secondary复制集节点——更精简的方式2
该方式优点:快速通过mongodump初始化数据库,大大减少新的secondary节点从头开始初始化的风险:网络壅塞.oplog.rs过期.耗时太长等. 还原的关键:一致性mongodump备份 + ...