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的更多相关文章

  1. Android 对话框(Dialog)大全 建立你自己的对话框

    Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...

  2. 转 Android 对话框(Dialog)大全 建立你自己的对话框

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  3. 95秀-自定义对话框 dialog 合集

    普通的确认对话框 NormalDialog.java import android.app.Dialog; import android.content.Context; import android ...

  4. Android 常用对话框Dialog封装

    Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示:       ...

  5. Android项目实战(三十二):圆角对话框Dialog

    前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话框的"确定"按钮 难点:1.对话框边框圆角 ...

  6. Android 对话框(Dialog)大全

    转自: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制, ...

  7. Android 对话框(Dialog)

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  8. Android 对话框(Dialog) 及 自己定义Dialog

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  9. (转载)Android项目实战(三十二):圆角对话框Dialog

    Android项目实战(三十二):圆角对话框Dialog   前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...

  10. android对话框(Dialog)的使用方法

    Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

随机推荐

  1. DOS下更改编码方式

    使用CHCP命令,CHCP是Change Code Page的缩写. 936 简体中文GBK 437 美国英语 65001 UTF编码 如:chcp 65001则将dos窗口中的字符编码改为UTF编码 ...

  2. 解决vmware安装 win7 后 没有虚拟网卡驱动 不能上网的问题

    项目需要用到win7 32位系统,于是装个虚拟机,换了好几个系统资源,都是没有网卡驱动, XP 2003 都能上网唯独WIN7 不行,安装vmware tools也不管用,终于找到了这个东西.vmwa ...

  3. 百度 UEditor 简单安装调试和调用,网上其它的教程太官方了,不适合新手

    对于新手来说,只要能实现功能即可,其它设置完全默认. 预览图: 1.首先 到官网下载,这个不多说.下载后解压到网站你想要的目录,我这里放到根目录下在你需要使用编辑器的地方,插入如下HTML代码: &l ...

  4. IS上部署MVC网站,打开后ExtensionlessUrlHandler-

    以管理员运行下面的命令注册: 32位机器: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 64位机器: C:\W ...

  5. 002-python基础-hello-world

    python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行. 如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py ,那么就需 ...

  6. JLINK V8 Keil MDK4.10 STM32

    新买的JLINK v8仿真器,第一次使用,编译环境是Keil MDK4.10,目前芯片是STM32F103x. 按照光盘的说明先安装了驱动,USB接上JLINK v8,显示驱动成功.但是在debug或 ...

  7. Oracle11g install Bbed

    1.sbbdpt.o ssbbded.o bbedus.msb文件链接地址: http://pan.baidu.com/s/1c0tHMCS 2.DB:  Oracle Database 11g En ...

  8. Collaborative filtering

        Collaborative filtering, 即协同过滤,是一种新颖的技术.最早于1989年就提出来了,直到21世纪才得到产业性的应用.应用上的代表在国外有Amazon.com,Last. ...

  9. IOS中的UINavigationController(导航控制器)

    UINavigationController UINavigationControlle:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个试图控制器 导航控制器可以认为是管理控制器的控制 ...

  10. 通过firefox+ProxySelector+dtunnel_lite实现代理上网

    通过firefox+ProxySelector+dtunnel_lite实现代理上网 dtunnel_lite:http://dog-tunnel.tk/下载lite版本就可以 远端:./dtunne ...