问题描述
Qt 5.0 提供了一个新的 QWindow
类.虽然关于这个类的文档非常全面,但我没有看到 QWindow
与 QWidget
类究竟有何不同,在哪些情况下您更喜欢前者.两者都提供了一种在屏幕上可视化各种事物的便捷方式,都可以使用 QPainter
进行绘图,并且都可以与 OpenGL 交互.
The Qt 5.0 provides a new QWindow
class. While the documentation on this class is quite comprehensive, I am failing to see how exactly the QWindow
is different from the QWidget
class, and in which cases you would prefer the former. Both provide a handy way of visualising all sorts of things to the screen, both can use QPainter
for drawing, and both have a way to interact with OpenGL.
在API描述中,它说:
应用程序通常将 QWidget 或 QQuickView 用于其 UI,而不是直接使用 QWindow.
所以这似乎不是窗口的优势.此外,它指出:
So that doesn't seem to be an advantage for the window. Moreover, it states:
Windows 可能会使用大量内存.通常的测量是宽度乘以高度乘以颜色深度.一个窗口还可能包含多个缓冲区以支持双缓冲和三缓冲,以及深度和模板缓冲区.
这似乎不赞成使用 QWindow
.那么在什么情况下你会使用它?
Which doesn't seem to be in favour of using the QWindow
. So in what cases would you use it?
推荐答案
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 QWindow
s 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. :)
这篇关于QWindow 和 QWidget 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!