【问题标题】:close QMessageBox when certain condition is fulfilled满足特定条件时关闭 QMessageBox
【发布时间】:2015-01-20 11:55:16
【问题描述】:

这是有效的:

#ifndef MYWARMUPMESSAGEBOX_H
#define MYWARMUPMESSAGEBOX_H

#include <QMessageBox>
#include <QPushButton>
#include <QTimer>

class myWarmUpMessageBox : public QMessageBox
{
    Q_OBJECT

private:

   QString _text;
   int _timeoutSeconds;
   QTimer _timer;
   int num = 0;

public:
explicit myWarmUpMessageBox(QWidget * parent):
   QMessageBox(parent)
   {
       connect(&_timer, SIGNAL(timeout()), this, SLOT(updateText()));
       _timer.start(500);
   }

   virtual void showEvent(QShowEvent * e)
   {
       QMessageBox::showEvent(e);
       updateText();
   }

public slots:

void updateText()
{
    num+=1;
    setText(QString::number(num));

    if(num>3)
        this->close();
}

我在关闭事件的 QMainWindow 中使用这个 QMessageBox。

void MainWindow::closeEvent(QCloseEvent *event)
{
    myWarmUpMessageBox * myBox = new myWarmUpMessageBox(this);
    myBox->exec();
    event->accept();
}

QMessageBox 弹出,计数到 3 消失,随后 QMainWindow 关闭。

BUT 如果立即满足关闭条件,即说

,则它不起作用
if(num>0)
    this->close();

第一次触发计时器时为真,程序会刹车。为什么???

【问题讨论】:

  • 当您的消息框变得可见时也是如此。 “程序刹车”,- 是什么意思?
  • 它冻结了。如果函数 updateText 至少被调用两次(即计时器超时两次),它不会冻结。

标签: c++ qt qmessagebox


【解决方案1】:

您必须让QMessageBox完全打开并最大化然后只需要关闭它,否则QMessageBox对话框可能尚未注册/完全加载,关闭功能才能成功。

如果您尝试访问 Dialog closeevent 中的任何 Dialog 小部件属性,closeevent 也是如此。

【讨论】:

  • 啊,你是对的!我想我必须等待任何事情,直到演出活动结束。所以我可能会用 show 事件启动一个计时器,然后更新文本。
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多