Qt菜单、文件、信号与槽最简单程序
来源:互联网 发布:cda数据分析师level1 编辑:程序博客网 时间:2024/06/07 11:05
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QTextEdit;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void openFile();
void saveFile();
private:
QAction *openAction;
QAction *saveAction;
QTextEdit *textEdit;
};
#endif// MAINWINDOW_H
#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);
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."));
}
}
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
<RCC>
<qresource prefix="/images">
<file alias="file-open">res/file-open.png</file>
<file alias="file-save">res/file-save.png</file>
</qresource>
</RCC>
阅读全文
0 0
- Qt菜单、文件、信号与槽最简单程序
- QT 信号与槽 最简单例子
- QT 信号与槽 最简单例子
- QT 信号与槽的简单例子
- 简单的Qt连接信号与槽
- QT信号与槽的简单实例
- Qt 信号与信号,信号与槽
- QT 信号与槽 QT简单加法器的实现
- qt 槽与信号
- QT信号与槽
- QT 信号与槽
- QT信号与槽
- Qt信号与槽
- Qt信号与槽
- QT信号与槽
- Qt信号与槽
- QT信号与槽
- QT信号与槽
- java基础之八大类型
- Hive 内建操作符与函数开发——深入浅出学Hive
- selenium pageObject
- Android 在 SElinux下 如何获得对一个内核节点的访问权限
- luogu1414 又是毕业季II
- Qt菜单、文件、信号与槽最简单程序
- BZOJ2661 / wc2012 连连看【费用流】
- JavaEE课程精讲
- pdf.js 利用HTML5技术显示pdf内容
- java后台报错:Unknown column 'xxx' in 'field list'
- appium+python一个脚本同时运行在多台android设备
- JSON学习小结
- Android源码分析之浅析Android系统启动过程
- ES6——Promise