小部件教程——创建窗口

如果小部件在创建时未指定父级,则在显示时会被视为窗口(即顶级小部件)。由于它没有父级对象来确保在不再需要时被删除,因此需要开发者自行跟踪应用程序中的顶级小部件。

在下面的示例中,我们使用 `QWidget ` 来创建并显示一个具有默认大小的窗口:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;
    window.resize(320, 240);
    window.show();
    window.setWindowTitle(
        QApplication::translate("toplevel", "Top-level widget"));
    return app.exec();
}
顶级控件应用程序窗口

要创建真正的图形用户界面(GUI),我们需要将控件放置在窗口内。为此,我们将QWidget 实例传递给控件的构造函数,本教程的下一部分将对此进行演示。

示例项目 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.