“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
When we use QT to do development will find that in many cases need to play some prompts to tell the user some information.
In general, we would think of using QMessageBox directly to prompt.
However, in some large projects, for better display, UI beauty will make some beautiful prompt page, if so, we need to define a prompt box class.
Blocking MessageBox messages is an obvious drawback, especially when our program is no longer controllable.
Suppose a larger project now requires a tablet to interact with a PC.
The tablet to trigger some operations on the PC, the simplest we can use TCP communication, so when the tablet triggers the MESSAGE error on the PC, do you think it is safe to block the way to play the prompt box?
Obviously, it must not be according to the actual situation.
So we need to do something to this prompt class that no longer inherits QDialogEx, but QWidget.
In order to make multiple pages compatible with the prompt class, do you want to create a new prompt class for each page?
In this way, it also consumes resources, and this approach is not adopted.
In our development project, we define a global prompt class called QTipsInforWidget
First, create the current prompt box class
QTipsInforWidget *m_TipsDlg = new QTipsInforWidget; m_TipsDlg->hide(); By default, the current page is not displayed and no parent window is assigned to it. That’s the point.
If you don’t specify the parent window, which page should you display it on?
When our window calls the class again, we specify the parent window information for the prompt box class
M_TipsDlg ->setTips(" this ", this);
We’re QTipsInforWidget: : setTips concrete operation, are as follows:
QTipsInforWidget::setTips(QString qsTips, QWidget* widgetParent) { this->setParent(widgetParent); }}
This operation realizes that multiple Windows can share a prompt box class, and the prompt box class belongs to the current window.
In fact, see here, many people say that a program only consider only play a prompt box, so if you want to sequential prompt information, or trigger multiple prompt information at one time, how to solve? (Haha, leave a question, this question will be explained next time ~)
The most important thing to remember is that when closing the main application, make sure that nullPtr is set as the parent of the prompt box, otherwise the application will crash.
Throughout the project, make sure there are unique new and unique DELETE, and that there is no parent window when new, and that the class is not attached to any window when we destroy it.
The specified parent window pointer is already a message, but you need the prompt box class to keep the parent pointer
Well, that’s all for today’s update, multiple Windows simply call a prompt box class message ~