Demo entry 6627549

Dialog

   

Submitted by xk on Jun 29, 2017 at 09:49
Language: C++. Code size: 2.7 kB.

// !!! Qt5


/*************************mainwindow.h****************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	MainWindow(QWidget *parent = 0);
	~MainWindow();
	
private:
	void open();
	
	QAction *openAction;
	
};

#endif // MAINWINDOW_H

/*************************mainwindow.cpp****************************/
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QToolBar>

#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent)
{
    setWindowTitle(tr("MainWindow"));

    openAction = new QAction(QIcon(":/images/doc-open") , tr("&Open...") , this);
    openAction -> setShortcut(QKeySequence::Open );
    openAction -> setStatusTip(tr("Open an existing file"));
    connect(openAction , &QAction::triggered , this , &MainWindow::open);

    QMenu *file = menuBar() -> addMenu(tr("&File"));
    file -> addAction(openAction);

	
    QToolBar *toolBar = addToolBar(tr("&File"));
    toolBar -> addAction(openAction);

    QToolBar *toolBar2 = addToolBar(tr("Tool Bar 2"));
    toolBar2 -> addAction(openAction);
	
    statusBar();

}

MainWindow::~MainWindow()
{

}

/**********************************************
Name: open - 1
Debug Result: 点击菜单栏/工具栏按钮,弹出模态对话框
**********************************************/
void MainWindow::open()
{
    //QMessageBox::information(this, tr("infomation"),tr("Open"));
	
	QDialog dialog;
	dialog.setWindowTitle(tr("HelloWorld !"));
	dialog.exec();
	
}

/**********************************************
Name: open - 2
Debug Result: 点击菜单栏/工具栏按钮,弹出模态对话框
**********************************************/
void MainWindow::open()
{
    //QMessageBox::information(this, tr("infomation"),tr("Open"));
	
	QDialog dialog(this);
	dialog.setWindowTitle(tr("HelloWorld !"));
	dialog.exec();
	
}

/**********************************************
Name: open - 3
Debug Result: 点击菜单栏/工具栏按钮,对话框一闪而过
**********************************************/
void MainWindow::open()
{
	QDialog dialog(this);
	dialog.setWindowTitle(tr("HelloWorld,dialog!"));
	dialog.show();
	
}

/**********************************************
Name: open - 4
Debug Result: 点击菜单栏/工具栏按钮,弹出非模态对话框
**********************************************/
void MainWindow::open()
{
    QDialog *dialog = new QDialog;
    dialog -> setWindowTitle(tr("HelloWorld,dialog"));
    dialog -> show();

}

/*
根据有无parent指针,对话框可分为模态对话框和非模态对话框

模态对话框:会阻塞同一应用程序中其他窗口的输入,
常见的“打开文件”,当打开文件对话框出现时,我们是不能对除此以外的创窗口进行操作,

非模态对话框:例如查找对话框,在显示对话框的同时,可继续选择其他窗口。
*/

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).