We were using the QT When doing development, you will find , In many cases, users need to pop up some information boxes .
In general, we think directly of , Use QMessageBox Just give a direct prompt .
But in some big projects , For better display effect ,UI The American trade union makes some nice prompt pages , If so , We need to define a prompt box class ourselves .
Especially when our program is no longer controllable , Obstructed MessageBox The news is obviously a drawback .
Suppose that a larger project needs a tablet and PC Machine interaction .
Triggered by the tablet PC Some operations of the machine , The simplest we can use TCP Communications , So when the tablet triggers PC Error in machine message , You think pop up the prompt box in a blocked way , Is this safe ?
Obvious , According to the actual situation, it is definitely not possible .
Then we need to deal with this prompt box class , No longer inheritance QDialogEx, It is QWidget 了 .
In order to make multiple pages compatible with this prompt box class , Do you want every page to new Such a prompt box class ?
It looks like , It also consumes more resources , This approach is also not taken .
In the project we are developing , Define a global prompt box class , Suppose it's called :QTipsInforWidget
First , Create the current prompt box class
QTipsInforWidget *m_TipsDlg = new QTipsInforWidget;
m_TipsDlg->hide();
The current page is not displayed by default , And don't assign any parent window to the . That's the point .
Then do not specify the parent window , On which page should it be displayed ?
When we which window calls this class again , Then specify the specific parent window information for the prompt box class
m_TipsDlg->setTips(" Prompt content ", this);
We are QTipsInforWidget::setTips The specific operation of , as follows :
QTipsInforWidget::setTips(QString qsTips, QWidget* widgetParent)
{
this->setParent(widgetParent);
// Specific practical operation
}
This kind of operation , Multiple windows can share a prompt box class , And the prompt box belongs to the current window .
Actually , See here , A lot of people say , A program only considers playing one prompt box at a time , So if you want to prompt information in sequence , Or trigger multiple prompt messages at one time , How to solve it ?( ha-ha , Leave a doubt first , This problem will be explained next time ~)
The most important thing to remember is , When closing the main program , Be sure to set the parent class of the prompt box to nullptr, Otherwise the program crashes and you have no way to start .
Throughout the project , Guarantee the only new And the only delete, also new When there is no parent window , Don't attach this class to any window when we destroy it .
Avoided , The specified parent window pointer has sent a message , But you have to prompt the box class to keep the parent pointer , It's strange not to collapse ~
Okay , That's all for today's update , Multiple windows simply call a prompt box class information ~
I am a good citizen of China st, a C++ Development program yuan ~
原网站版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070909198385.html