[PySide] Difference between QMainWindow QWidget and QDialog

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Tue Mar 10 17:38:08 CET 2020


Hello,

On 3/10/20 10:11 AM, JZA wrote:
> Sort of want to know the difference between the different widgets and 
> when to use the one that is needed. Also if there is any difference 
> codewise for creating and launching the class.


Everything in Qt comes from the QObject class [1],
and all the Q* classes inhetit, at some level, from it.

So besides QtCore, there are other foundational modules
like QtGui, which provide all the classes related to the UI,
but not widgets, more like the action of drawing certain things
on the screen, from which an important one is called QPaintDevice [2].

Going one level up, you have the module QtWidgets, which has
as base the QWidget[3] class,
which inherits from QObject and QPaintDevice, and it's the starting
point of all the other Widgets you already heard about, like
QMainWindow, and QDialog.

A QWidget can be filled manually by other QWidgets, like buttons,
labels, combo box, toolbars, status, bar, etc, to create a "real 
application window", but since that's scenario is so common
there was a decision to create a really high-level widget
that contains many common "Sections" that one could configure,
that's QMainWindow [4].

As you can see on the "Detailed Description" diagram,
it already provides you with some areas you can interact with,
so if you want to build a standard window application that follows
that pattern, you can use it, otherwise you would need to build
one by yourself.


All widgets based on QWidget can be constructed and showed as:

w = QSomeWidget()
w.show()

and that's the definition of Modeless widget,
since it's independent of other windows.


A QDialog, on the other hand, is a top level windows
which was design for short interactions with the user,
but it's not only Modeless, it can be modal too,
and for it to behave like a Modal widget, it needs to be executed
by the exec_() method, and not the show() one.


tl;dr:
- QMainWindow: for using the same pre-made structure
- QWidget: for displaying something simple or building your own thing,
- QDialog: for short interactions Modal or Modeless

Cheers



[1] https://doc.qt.io/qt-5/qobject.html
[2] https://doc.qt.io/qt-5/qpaintdevice.html
[3] https://doc.qt.io/qt-5/qwidget.html
[4] https://doc.qt.io/qt-5/qmainwindow.html



-- 
Dr. Cristian Maureira-Fredes
R&D Manager

The Qt Company GmbH
Erich-Thilo-Str. 10
D-12489 Berlin

Geschäftsführer: Mika Pälsi,
Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht
Charlottenburg, HRB 144331 B
--


More information about the PySide mailing list