سؤال

I need to start a job when QMainWindow and all its widgets are initialized and rendered.

How can I catch such event?

هل كانت مفيدة؟

المحلول

I see two ways of doing this.

Sophisticated:

void MainWindow::showEvent(QShowEvent *e)
{
    QMainWindow::showEvent(e);
    static bool firstStart = true;
    if (firstStart)
    {
        emit startJob();
        firstStart = false;
    }
}

And easy one (suitable only, if you show main window right after creating):

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    ...
    QTimer::singleShot(500, this, SLOT(job()));
}

Update:

Like Chris said, showEvent is much more appropriate here, than paintEvent.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top