简体   繁体   English

QWidget和QMessageBox的显示

[英]QWidget and showing of QMessageBox

I have a QWidget which shows a QMessageBox on the show event. 我有一个QWidget,它在show事件中显示一个QMessageBox。 I have overridden the showEvent function for the QWidget. 我已经覆盖了QWidget的showEvent函数。 The problem is, that the messagebox is displayed first, and the rest of the widget is displayed later. 问题是,首先显示消息框,稍后显示小部件的其余部分。 How do I resolve this problem? 我该如何解决这个问题?

void InstallScreen::showEvent( QShowEvent *s )
{ 
   QMessageBox::about( m_main, "One Click Installer", 
          QString( "The following repositories will be added \n %1" ).arg( repoList ) ); 
   QMessageBox::about( m_main, "One Click Installer", 
          QString( "The following packages will be installed \n %1" ).arg( packList ) ); 
} 

Use a delayed call to show the widget, for instance by putting the widget show code in a slot and calling it with QTimer::singleShot with a delay of 0 secs. 使用延迟调用来显示窗口小部件,例如将窗口小部件显示代码放在插槽中并使用QTimer :: singleShot调用它,延迟为0秒。

This will allow the showEvent to process fully and schedule any redraw events on the mainloop before you show your widget. 这将允许showEvent 显示窗口小部件之前完全处理并在mainloop上安排任何重绘事件。 This will make the widget & messagebox to be shown/drawn/repainted independently of each other. 这将使小部件和消息框彼此独立地显示/绘制/重新绘制。

protected:
void MyWidget::showEvent(...) {
     ...
     QTimer::singleShot(0, this, SLOT(showMessageBox());
}

private slots:
void MyWidget::showMessageBox() {
     QMessageBox::information(...); // or whatever
}

If you need some extra margin (for whatever reason), set the timer delay to 50 or 100 ms. 如果您需要一些额外的余量(无论出于何种原因),请将定时器延迟设置为50或100 ms。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM