对话框Dialog
QMainWindow
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);
QDialog
Qt 中使用QDialog
类实现对话框
QDialog
(及其子类,以及所有Qt::Dialog
类型的类)的对于其 parent 指针都有额外的解释:如果 parent 为 NULL,则该对话框会作为一个顶层窗口,否则则作为其父组件的子对话框(此时,其默认出现的位置是 parent 的中心)。顶层窗口与非顶层窗口的区别在于,顶层窗口在任务栏会有自己的位置,而非顶层窗口则会共享其父组件的位置。
QDialog dialog; //顶层,在任务栏显示
QDialog dialog(this) //子窗口,在任务栏不显示
模态和非模态
对话框分为模态对话框和非模态对话框。所谓模态对话框,就是会阻塞同一应用程序中其它窗口的输入。模态对话框很常见,比如“打开文件”功能。你可以尝试一下记事本的打开文件,当打开文件对话框出现时,我们是不能对除此对话框之外的窗口部分进行操作的。与此相反的是非模态对话框,例如查找对话框,我们可以在显示着查找对话框的同时,继续对记事本的内容进行编辑。
Qt 支持模态对话框和非模态对话框。其中,Qt 有两种级别的模态对话框:应用程序级别的模态和窗口级别的模态,默认是应用程序级别的模态。应用程序级别的模态是指,当该种模态的对话框出现时,用户必须首先对对话框进行交互,直到关闭对话框,然后才能访问程序中其他的窗口。窗口级别的模态是指,该模态仅仅阻塞与对话框关联的窗口,但是依然允许用户与程序中其它窗口交互。
Qt 使用QDialog::exec()
实现应用程序级别的模态对话框,使用QDialog::open()
实现窗口级别的模态对话框,使用QDialog::show()
实现非模态对话框。
模态:
void MainWindow::open()
{
QDialog dialog;
dialog.setWindowTitle(tr("Hello"));
dialog.exec();
}
非模态:注意必须用new创建否则一闪而过,这是因为,show()
函数不会阻塞当前线程,对话框会显示出来,然后函数立即返回,代码继续执行。而用new是建立在堆上,执行完后不会消失。
不过,这样做有一个问题:如果我们的对话框不是在一个界面类中出现呢?由于QWidget
的 parent 必须是QWidget
指针,那就限制了我们不能将一个普通的 C++ 类指针传给 Qt 对话框。另外,如果对内存占用有严格限制的话,当我们将主窗口作为 parent 时,主窗口不关闭,对话框就不会被销毁,所以会一直占用内存。在这种情景下,我们可以设置 dialog 的WindowAttribute
:
void MainWindow::open()
{
QDialog *dialog = new QDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setWindowTitle(tr("Hello, dialog!"));
dialog->show();
}
内置对话框
QColorDialog
:选择颜色;QFileDialog
:选择文件或者目录;QFontDialog
:选择字体;QInputDialog
:允许用户输入一个值,并将其值返回;QMessageBox
:模态对话框,用于显示信息、询问问题等;QPageSetupDialog
:为打印机提供纸张相关的选项;QPrintDialog
:打印机配置;QPrintPreviewDialog
:打印预览;QProgressDialog
:显示操作过程。
QMessageBox
QMessageBox
用于显示消息提示。我们一般会使用其提供的几个 static 函数:
显示关于对话框。这是一个最简单的对话框,其标题是 title,内容是 text,父窗口是 parent。对话框只有一个 OK 按钮
void about(QWidget * parent, const QString & title, const QString & text)
显示关于 Qt 对话框。该对话框用于显示有关 Qt 的信息
void aboutQt(QWidget * parent, const QString & title = QString())
显示严重错误对话框。这个对话框将显示一个红色的错误符号。我们可以通过 buttons 参数指明其显示的按钮。默认情况下只有一个 Ok 按钮,我们可以使用StandardButtons
类型指定多种按钮
StandardButton critical(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
QMessageBox::information()
函数与QMessageBox::critical()
类似,不同之处在于这个对话框提供一个普通信息图标
StandardButton information(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
QMessageBox::question()
函数与QMessageBox::critical()
类似,不同之处在于这个对话框提供一个问号图标,并且其显示的按钮是“是”和“否”两个
StandardButton question(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)
QMessageBox::warning()
函数与QMessageBox::critical()
类似,不同之处在于这个对话框提供一个黄色叹号图标
StandardButton warning(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
QFileDialog文件对话框
一个txt编辑保存的实例
主窗口继承mainwindow,#include <QtWidgets> 包含了QT的常用控件(比如QTextEdit,QAction)
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
main.cpp
#include <QApplication>
#include "mainwindow.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv); MainWindow w;
w.show(); return a.exec();
}
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); 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."));
}
}
对话框Dialog的更多相关文章
- Android 对话框(Dialog)大全 建立你自己的对话框
Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...
- 转 Android 对话框(Dialog)大全 建立你自己的对话框
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- 95秀-自定义对话框 dialog 合集
普通的确认对话框 NormalDialog.java import android.app.Dialog; import android.content.Context; import android ...
- Android 常用对话框Dialog封装
Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示: ...
- Android项目实战(三十二):圆角对话框Dialog
前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话框的"确定"按钮 难点:1.对话框边框圆角 ...
- Android 对话框(Dialog)大全
转自: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制, ...
- Android 对话框(Dialog)
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- Android 对话框(Dialog) 及 自己定义Dialog
Activities提供了一种方便管理的创建.保存.回复的对话框机制,比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- (转载)Android项目实战(三十二):圆角对话框Dialog
Android项目实战(三十二):圆角对话框Dialog 前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...
- android对话框(Dialog)的使用方法
Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
随机推荐
- Java实现计算20的阶乘
循环从1乘到20,要注意的就是结果可能会很大,长度超出int类型的范围,所以定义乘积的时候用long. 代码如下: public class Practice3 { public static voi ...
- SQL Server中查询结果拼接遇到的小问题
前天的项目,刚接手,对于模块还不是很熟悉,其中有一个模块,涉及到4个表,其中主要的表就有两个,只要把这个弄清楚了就一切回归于“太平”了. 模块要求:把两个表的内容查询出来,结果连接在一起.大师说完,感 ...
- How to using to code import to GL journal[AX2012]
static void THK_importLedgerJournalTrans(Args _args) { Filename fileName = "C:\\Users\\ksiu3880 ...
- Java 装箱 拆箱
Java 自动装箱与拆箱 ??什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我 ...
- Ubuntu 下安装 Oracle JDK
sudo add-apt-repository ppa:webupd8team/javasudo apt-get updatesudo apt-get install oracle-java8-ins ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
- openSUSE13.1 Yast 中所有软件图形化界面无法打开,问题原因: Ruby
因为使用rvm安装了新的Ruby,而openSUSE13.1的YaST又是用Ruby的.....解决方案暂时没有
- SQL注入式攻击
百度百科:http://baike.baidu.com/link?url=GQbJ2amTzTahZA7XJSBDLYYkN3waQ9JCoJ0l--tCWlvKQibe0YaH4hpmgEnLyn0 ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- 用Java实现3DES
3DES,即三重DES,是DES的加强版,也是DES的一个更安全的变形.它使用3个56位(共168位)的密钥对数据进行三次加密,和DES相比,安全性得到了较大的提高. 实际上,3DES是一个过渡的加密 ...