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> ...
随机推荐
- Linux网络的设置
一.介绍 目的:使Linux可以正常上网,前提是物理机可以上网 软件环境: 虚拟机版本: VMware Workstation 12, Linux系统版本:CentOS 7.3 二.设置网络 1,在登 ...
- 第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码
第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码 打码接口文件 # -*- coding: cp936 -*- import sys import os ...
- elasticsearch系列六:聚合分析(聚合分析简介、指标聚合、桶聚合)
一.聚合分析简介 1. ES聚合分析是什么? 聚合分析是数据库中重要的功能特性,完成对一个查询的数据集中数据的聚合计算,如:找出某字段(或计算表达式的结果)的最大值.最小值,计算和.平均值等.ES作为 ...
- 嵌入式开发之zynq---Zynq PS侧DMA驱动
http://xilinx.eetrend.com/blog/10760 http://xilinx.eetrend.com/blog/10787
- nodejs基础 -- express框架
Node.js Express 框架 Express 简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP ...
- EasyUI的combobox组件Chrome浏览器不兼容问题解决办法
EasyUI版本:jQuery EasyUI 1.4.1 Chrome浏览器版本:41.0.2272.101 m 问题描述 在Chrome浏览器下,下拉框选择选项之后,选择的值在下拉框中不显示,重新选 ...
- memcached系列之
Slab Allocator的机制分配.管理内存 slabs---->slabs class:chunk size------>申请内存后分配的规格. chunk-->存放记录的单位 ...
- touch事件的分发和消费机制
Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- mocha框架下,异步测试代码错误造成的问题----用例超时错误
今天用抹茶(mocha)做个测试,发现有一个测试项目总是超时: describe("DbFactory functions",function(){ it("query ...
- Java编程思想学习笔记——初始化与清理(二)
成员初始化 Java尽力保证:所有变量在使用前都能得到适当的初始化. 方法的局部变量:未初始化,编译错误. void f(){ int i; // System.out.println(i);//编译 ...