且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

QWindow 和 QWidget 有什么区别

更新时间:2023-02-19 14:30:17

QWindow 由于 gui/widgets 拆分,已在 Qt 5.0 中引入.QWidget 现在存在于它自己的库中(QtWidgets);有必要为基于非小部件的应用程序提供***窗口"的抽象,因此创建了 QWindow -- 并存在于 QtGui 中.

QWindow has been introduced in Qt 5.0 due to the gui / widgets split. QWidget now lives in its own library (QtWidgets); it was necessary to provide the abstraction of a "toplevel window" for non-widgets based applications, and thus QWindow was created -- and lives in QtGui.

有一整类非基于小部件的应用程序:所有使用 QtQuick2 的应用程序.它们根本不使用 QtWidget 库,事实上,在使用它们时,您总是以某种方式明确地使用 QWindows(QQuickView 继承自 QWindow).

There is an entire class of non-widgets based applications: all those using QtQuick2. They don't use the QtWidget library at all, and as a matter of fact, when using them you're always somehow using QWindows explicitely (QQuickView inherits from QWindow).

即使在使用小部件时,Qt 内核也会为您创建*** QWindows,这也使此类 QWindow 对象的属性和标志与相应的*** QWidget 保持同步.通过这种方式,您可以像往常一样处理小部件,而根本不了解 QWindow.现有应用程序将继续按预期运行,等等.

Even when using widgets, top-level QWindows get created for you by the Qt kernel, which also keeps the properties and the flags of such QWindow objects in sync with the corresponding top-level QWidgets. This way you can just deal with widgets like you always did, without knowing about QWindow at all. Existing applications will continue to run as expected, etc. etc.

我一直在使用 QWindow 的唯一原因(到目前为止)是为了一个非常具体的用例:绘制纯 OpenGL 内容.这很容易实现(通过在窗口上设置 OpenGL 表面类型),并避免您引入额外的依赖项(QtWidgets、QtOpenGL 等,它们在库大小方面有成本);它允许用大约 10 行代码创建一个 OpenGL 绘图表面 这将适用于 Linux、Windows、Mac、QNX、嵌入式 Linux",并且很可能也适用于 Android 和 iOS. 从这个角度来看,它是一个完美的 SDL 替代品.:)

The only reason (so far) I've been using QWindows explicitely is for a very specific use case: to draw pure OpenGL content. This is very easy to achieve (by setting an OpenGL surface type on the window), and avoids you to bring in additional dependencies (QtWidgets, QtOpenGL, etc., which have a cost in terms of library size); it allows to create a OpenGL drawing surface in like 10 lines of code which will work on Linux, Windows, Mac, QNX, "embedded Linux", and very likely Android and iOS too. From this point of view it acts as a perfect SDL replacement. :)