当前位置:网站首页>Dialog introduction
Dialog introduction
2022-07-27 05:01:00 【PHP code】
The dialog is GUI An integral part of the process . Many function components that cannot or are not suitable for the main window must be set in the dialog box . A dialog box is usually a top-level window , At the top of the program , For short-term tasks or simple user interaction . Even though Ribbon The appearance of the interface reduces the probability of using the dialog box to a certain extent , however , We can still use the latest version Office Found many dialog boxes in . therefore , For the foreseeable future , Dialog boxes will always exist in our program .
Qt Use in QDialog Class implementation dialog . Just like the main window , We usually design a class inheritance QDialog.QDialog( And its subclasses , And all Qt::Dialog Class of type ) For its parent Pointers have extra explanations : If parent by NULL, The dialog box will act as a top-level window , Otherwise, as the child dialog of its parent component ( here , The default location is parent Center of ). The difference between a top-level window and a non top-level window is , The top-level window will have its own place in the taskbar , The non top level window shares the location of its parent component .
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("Main Window"));
openAction = new QAction(QIcon(":/images/doc-open"), tr("&Open..."), this);
openAction->setShortcuts(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);
}
MainWindow::~MainWindow()
{
}
void MainWindow::open()
{
QDialog dialog;
dialog.setWindowTitle(tr("Hello, dialog!"));
dialog.exec();
}
Above we use the previous example code . Notice that open() What's in the function . We use QDialog Created a dialog , Set its title to “Hello, dialog!”, And then call exec() Show it . Notice the icon of the taskbar , Because we didn't set the dialog parent The pointer , We will see where the dialog box appears in the taskbar :

So let's revise that open() The contents of the function :
void MainWindow::open()
{
QDialog dialog(this);
dialog.setWindowTitle(tr("Hello, dialog!"));
dialog.exec();
}
Run it again , A comparison will show parent Whether the pointer is right QDialog The impact of the instance .
Dialog box is divided into modal dialog box and non modal dialog box . So called modal dialog , It will block the input of other windows in the same application . Modal dialog boxes are common , such as “ Open file ” function . You can try Notepad's open file , When the open file dialog box appears , We can't operate the window except this dialog box . In contrast, modeless dialog boxes , For example, the Find dialog , We can display the search dialog while , Continue to edit the contents of Notepad .
Qt Support modal dialog and non modal dialog . among ,Qt There are two levels of modal dialog : Application level mode and window level mode , The default is application level mode . Application level mode refers to , When the dialog box of this mode appears , The user must first interact with the dialog , Until you close the dialog , Then you can access other windows in the program . Window level mode refers to , This mode only blocks the window associated with the dialog , But it still allows users to interact with other windows in the program . Window level mode is especially suitable for multi window mode , A more detailed discussion can be seen Previously published articles .
Qt Use QDialog::exec() Implement application level modal dialog , Use QDialog::open() Realize the modal dialog box at the window level , Use QDialog::show() Implementation of non modal dialog . Review our code , In the example above , We call exec() Show the dialog box , So this is a modal dialog . When the dialog box appears , We can't have any interaction with the main window , Until we close the dialog .
Now let's try to exec() It is amended as follows show(), Look at the modeless dialog :
void MainWindow::open()
{
QDialog dialog(this);
dialog.setWindowTitle(tr("Hello, dialog!"));
dialog.show();
}
Is it the opposite of what you want ? The dialog box just flashed by ! This is because ,show() Function does not block the current thread , The dialog box will be displayed , Then the function immediately returns , The code continues to execute . Be careful ,dialog It's built on the stack ,show() The function returns ,MainWindow::open() End of the function ,dialog Out of scope is destructed , So the dialog disappears . It's easy to change if you know the reason , We will dialog It's built on the pile , Of course, there is no such problem :
void MainWindow::open()
{
QDialog *dialog = new QDialog;
dialog->setWindowTitle(tr("Hello, dialog!"));
dialog->show();
}
Compare this modeless dialog with the previous modal dialog . We can interact with the main window when the dialog box appears , So we can create multiple identical dialog boxes :

If you are careful enough , It should be found that there is something wrong with the code above :dialog There is a memory leak !dialog Use new Allocate space on the heap , But never delete. The solution is simple : take MainWindow The pointer to the dialog that will do . Remember what we said before Qt Object system of ?
however , There's a problem with doing this : If our dialog does not appear in an interface class ? because QWidget Of parent Must be QWidget The pointer , That limits us from putting an ordinary C++ Class pointer to Qt Dialog box . in addition , If there are strict limits on memory usage , When we use the main window as parent when , The main window doesn't close , The dialog will not be destroyed , So it takes up memory all the time . In this situation , We can set dialog Of WindowAttribute:
void MainWindow::open()
{
QDialog *dialog = new QDialog;
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setWindowTitle(tr("Hello, dialog!"));
dialog->show();
}
setAttribute() When the function settings dialog box closes , Auto destroy dialog . in addition ,QObject One more deleteLater() function , This function destroys the dialog box at the end of the current event loop ( That's it , Need to use exec() Start a new event cycle ). About the cycle of events , We will explain in detail in the following article .
边栏推荐
- HCIA dynamic routing rip basic experiment
- 使用ngrok做内网穿透
- 【报错】:Cannot read properties of undefined (reading ‘prototype‘)
- Final Cut Pro Chinese tutorial (2) understanding of material window
- TCP's three handshakes and four waves
- 柔性数组以及常见问题
- Huawei's entry into the commercial market: due to the trend, there are many challenges
- 缓存读写策略:CacheAside、Read/WriteThrough及WriteBack策略
- strlen和sizeof的区别
- 消防安全培训资料汇总
猜你喜欢

消防安全培训资料汇总

利用Power Automate,轻松下载Power BI报告中的数据

如何重置Photoshop首选项?ps重置首选项的方法

【搜索】—— 多源BFS + 最小步数模型

数字中国建设峰会闭幕,现场海量图片一览!

使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“

【报错】Cannot read property ‘parseComponent‘ of undefined

R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on

TCP three handshakes and four disconnects

HCIA dynamic routing OSPF experiment
随机推荐
一道数学题,让芯片巨头亏了5亿美金
feign调用丢失请求头问题解决及原理分析
STL upper series - detailed explanation of list container
strlen和sizeof的区别
写代码涉及到的斜杠/和反斜杠\
vim的基本操作
2019 top tennis cup upload
HCIA dynamic routing rip basic experiment
如何将Photoshop图层复制到其他文档
使用ngrok做内网穿透
HCIA static routing comprehensive experiment
对话框简介
Plato farm is expected to further expand its ecosystem through elephant swap
【报错】:Cannot read properties of undefined (reading ‘prototype‘)
Customize the viewport height, and use scrolling for extra parts
【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普
勤于奋聊聊现在还有哪些副业可以做
There is no need to install CUDA and cudnn manually. You can install tensorflow GPU through a one-line program. Take tensorflow gpu2.0.0, cuda10.0, cudnn7.6.5 as examples
The usage syntax and scene of selector, as well as the usage of background picture size, text box shadow and excessive effect
事件过滤器