Qt: 主窗口->show(); 不显示主窗口

dfe*_*r88 3 c++ qt

在下面的代码中,我创建了一个窗口 invoke ,但该窗口直到调用window.show()后才显示。window->iterateSolution()几乎就像app.exec()是显示窗口的功能。我对 Qt 很陌生,所以我不知道发生了什么。

#include <QtGui/QApplication>
#include <mainWindow.h>
#include <Cube.h>

mainWindow * newWindow;

int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    QApplication app(argc, argv);
    newWindow = new mainWindow;
    newWindow->show();

    QString initialState = "YWOBYYBYYGRRGRRBWWYOOYGGRGGBBGYOOYOOWRRBBRBBWGOOGWWRWW";

    /* Construct cube, set state, and solve */
    Cube * cube = new Cube(initialState);
    QString solution = cube->solve();
    delete cube;
    newWindow->iterateSolution(solution);

    // create and show your widgets here

    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

Eti*_*tel 5

这正是正在发生的事情。

从技术上讲,QMainWindow::show() 并不使窗口可见,它只是在窗口中设置一个标志,Qt 将使其在事件循环的下一次迭代中可见。

另外,直接来自 Qt 关于 QApplication::exec() 的文档:

需要调用该函数来启动事件处理。主事件循环接收来自窗口系统的事件并将这些事件分派给应用程序小部件。

通常,在调用 exec() 之前不能进行任何用户交互。

  • 将 mainWindow-&gt;show() 到 app.exec() 之间的代码放在 mainWindow 上的“run”槽中,并使用 QTimer::singleShot(0, mainWindow, SLOT(run())) 使其从事件执行环形。 (2认同)