Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a QWindow and QWidget

Tags:

qt5

qwidget

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.

In the API description, it says that:

An application will typically use QWidget or QQuickView for its UI, and not QWindow directly.

So that doesn't seem to be an advantage for the window. Moreover, it states:

Windows can potentially use a lot of memory. A usual measurement is width times height times color depth. A window might also include multiple buffers to support double and triple buffering, as well as depth and stencil buffers.

Which doesn't seem to be in favour of using the QWindow. So in what cases would you use it?

like image 469
Yellow Avatar asked Jul 25 '13 14:07

Yellow


People also ask

What is the difference between QWidget and QMainWindow?

A QDialog is based on QWidget , but designed to be shown as a window. It will always appear in a window, and has functions to make it work well with common buttons on dialogs (accept, reject, etc.). QMainWindow is designed around common needs for a main window to have.

What is QWidget?

The QWidget class is the base class of all user interface objects. The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order.

Is QMainWindow a QWidget?

QWidget QMainWindow. Returns the central widget for the main window.

What is QWidget update?

This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does. Calling update() several times normally results in just one paintEvent() call.


1 Answers

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.

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).

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.

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. :)

like image 95
peppe Avatar answered Sep 21 '22 12:09

peppe