对话框Dialog
2015-12-22 14:26
357 查看
[b]QMainWindow[/b]
主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口。通常是由一个标题栏,一个菜单栏,若干工具栏和一个任务栏。在这些子组件之间则是我们的工作区。
通过添加动作来添加菜单和工具栏等,比如添加一个打开菜单和工具
View Code
QMainWindow是 Qt 框架带来的一个预定义好的主窗口类。
主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口。通常是由一个标题栏,一个菜单栏,若干工具栏和一个任务栏。在这些子组件之间则是我们的工作区。
通过添加动作来添加菜单和工具栏等,比如添加一个打开菜单和工具
QAction *openaction; openaction = new QAction(QIcon(":/img/open"),tr("&Open"),this); openaction->setShortcut(QKeySequence::Open); //给菜单指定快捷键 openaction->setStatusTip(tr("open an existing file")); //提示 //加入菜单栏的File中 QMenu *file = menuBar()->addMenu(tr("&File")); file->addAction(openaction); //加入工具栏 QToolBar *toolBar = addToolBar(tr("&File")); toolBar->addAction(openaction);
#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.")); } }
View Code
相关文章推荐
- 解决 java 中引用的jar包乱码问题
- 为了使JSON编码解码支持GB2312字符
- selenium webdriver 学习总结-JUnit4 入门(三)-补充枚举
- 对android应用进行单元测试
- Android 控件之点击链接跳转Activity
- Privoxy下forward-socks5和forward-socks5t的区别
- sparkuser is not in the sudoers file. This incident will be reported.
- 六、solr 查询参数说明
- Appium的一点一滴:Appium测试hybirdapp,在webView中处理方法
- PHP学习的一种习惯
- Python爬虫笔记
- json转义反斜杠
- 深入理解Android Studio之Gradle
- 常见RGB 颜值表和半透明颜色值的写法
- 《我是一只IT小小鸟》读书杂记
- cocospods的安装与应用
- 自定义progressbar
- Tomcat 7遇到StackOverflowError的异常的解决办法
- 基于Android热点的局域网UDP广播,部分手机收不到UDP报文的问题
- 关于fbx转化为md5