[Qt-interest] Wait for EventLoop in QMainWindow constructor

Teaburon teaburon at googlemail.com
Fri Jul 23 18:05:58 CEST 2010


Hello!

I want to call a method from my QMainWindow constructor that does something
with the gui. So I need to wait for the event loop to be ready before
calling the method.
In the web I found a way to do it with a single shot timer. This works for
me, if I set the timer to 100ms. It does not work if the timer is set to
0ms. That makes me worry about slower systems that run my programm... Is
there a better solution for this? I find it hard to believe that QT does not
provide something for this.

class MainWindow : public QMainWindow
{
  Q_OBJECT
public:
  MainWindow(QWidget *parent = 0);
  ~MainWindow();
private slots:
  void doStuff(); // This slot will contain your code
}
MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent), ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  QTimer::singleShot(0, this, SLOT(doStuff())); // This will call your
slot when the event loop is ready
}

void MainWindow::doStuff()
{
  // This code will run as soon as the event loop is ready
}

(http://stackoverflow.com/questions/2752893/is-there-a-way-to-implement-onready-callback-in-qt4)



Thank you,
Teaburon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100723/1bf6723d/attachment.html 


More information about the Qt-interest-old mailing list