Q桥接和显示QMessageBox

QWidget and showing of QMessageBox

本文关键字:QMessageBox 显示 桥接      更新时间:2023-10-16

我有一个QWidget,它在show事件上显示一个QMessageBox。我已经覆盖了QWidget的showEvent函数。问题是,消息框首先显示,小部件的其余部分稍后显示。我该如何解决此问题?

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 ) ); 
} 

使用延迟调用来显示小部件,例如,将小部件显示代码放在插槽中,并使用QTimer::singleShot以0秒的延迟调用它。

这将允许showEvent在显示小部件之前完全处理并安排主循环上的任何重绘事件。这将使小工具&消息框相互独立显示/绘制/重新绘制。

protected:
void MyWidget::showEvent(...) {
     ...
     QTimer::singleShot(0, this, SLOT(showMessageBox());
}
private slots:
void MyWidget::showMessageBox() {
     QMessageBox::information(...); // or whatever
}

如果您需要一些额外的余量(无论出于何种原因),请将计时器延迟设置为50或100毫秒。