0

This is working:

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

I am using this QMessageBox in a QMainWindow in its close event.

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

The QMessageBox pops up, counts to 3 diappears, and subsenquently the QMainWindow closes.

BUT it is not working if the condition for closing is fulfilled immediately, i.e. when saying

if(num>0)
    this->close();

which is true when the timer is triggered the first time, the program brakes. Why???

newandlost
  • 935
  • 2
  • 10
  • 21
  • It is also true, when your message box becomes visible. " the program brakes", - what does it mean? – vahancho Jan 20 '15 at 12:02
  • It freezes. It does not freez, if the function updateText is called at least twice (i.e. the timer times out twice). – newandlost Jan 20 '15 at 12:39

1 Answers1

0

You have to let the QMessageBox to open fully and maximized and then only you need to close it, otherwise the QMessageBox Dialog may not be registered/fully loaded yet, for the close function to be successful.

same goes for closeevent also, if you try to access any Dialog widget properties inside Dialog closeevent.

Sridhar Nagarajan
  • 1,085
  • 6
  • 14
  • ah you are right! I guess I have to wait with anything until the show event is over. So I probably start a timer with the show event which then is updating the text. – newandlost Jan 21 '15 at 18:00