创建一个没有父线程的新 Qt Window 是否也会创建一个新线程?

Does creating a new Qt Window with no parent also create a new thread?

我的问题分为两部分:

  1. mainWindow 是否在 运行 main() 以外的线程中生成?
  2. 当 main() returns 时,mainWindow 如何不立即超出范围?

在下面的例子中,一个 window 被创建,显示,并且 main returns 几乎立即被创建。

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

MainWindowExample.pro

#-------------------------------------------------
#
# Project created by QtCreator 2016-05-23T10:55:03
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MainWindowExample
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui
  1. 没有为 mainWindow
  2. 创建新线程
  3. mainWindow 事件循环在 a.exec() 范围内执行 - 它会阻塞直到应用程序退出(例如 - 最后一个顶级 window 已关闭)。

所以 mainWindow 不会超出范围,因为 main 执行所有操作。

使用如下代码进行检查:

std::cout << "starting application event loop" << std::endl;
const int ret = a.exec();
std::cout << "after exec" << std::endl; // or any other code here
return ret;

来自 QApplication 文档:

Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

mainWindow 是否在 运行 main() 以外的线程中生成?

没有。事实上,这样做是未定义的行为。只能在主线程中构造QWidget个后代。

Threads and Objects 文档主题包括:

Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.*

main() returns 时,mainWindow 如何不立即超出范围?

但确实如此!

main() returns 当 a.exec() returns 时,只有当您退出应用程序时才会发生这种情况。默认情况下,您的应用程序会在最后一个可见 window 关闭时自动退出。