The QAppliccation main event loop does not start until line 6 (a.exec()), so inside your MainWindow constructor there is no event loop yet. In addition, MainWindow "w" is actually not visible when you call w.show() in line 5, but only become visibles when the event loop starts in line 6.
So basically, nothing happens in A_panel::setup() because 1) there is no event loop and 2) your MainWindow constructor has never exited (you jumped out of it to call A_panel::setup() and your processEvents loop never exits), so you will never get to lines 5 or 6.
The way you use a QTimer is not good, because the timing will depend on the PC and on the load of other processes running at the time. So maybe 16ms is OK one time, but not good another time. You can use QTimer a different way.
Implement a slot for your MainWindow class:
// .h
class MainWIndow //...
{
protected slots:
void onStart();
//...
};
// .cpp
MainWindow
::MainWindow(QWidget *parent
) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer::singleShot( 0,
this,
SLOT( onStart
() );
}
void MainWindow::onStart()
{
new_panel= new A_panel(this);
new_panel->w_setup();
}
// .h
class MainWIndow //...
{
protected slots:
void onStart();
//...
};
// .cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer::singleShot( 0, this, SLOT( onStart() );
}
void MainWindow::onStart()
{
new_panel= new A_panel(this);
new_panel->w_setup();
}
To copy to clipboard, switch view to plain text mode
The timer will be started with a zero timeout just before the MainWindow constructor exits. However, because the event loop isn't running yet, the timeout() signal can't be processed until the event loop starts. After you call a.exec(), the event loop is then running and the timeout signal will get queued up. The MainWindow isn't necessarily visible in onStart(), but I think the event loop in w_setup() should probably work then and eventually the MainWindow will become visible.
Bookmarks