When may use QMainWindow instead of QDialog ?
-
@AndrzejB said in When may use QMainWindow instead of QDialog ?:
When is better using QMainWindow over QDialog?
Neither is "better", it spends what you facilities you want.
QDialog
is plain,QMainWindow
has a number of "areas" reserved on it for menus, toolbars etc. as per the diagram at https://doc.qt.io/qt-5/qmainwindow.html#details. If you do not want any of thoseQMainWindow
is overkill.If I change QDIalog to QMainWindow, all widgets are invisible.
Then your code is wrong. For a
QMainWindow
as per Creating Main Window Components you must useNote: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
You set the central widget withsetCentralWidget()
. -
setCentralWidget must be pointer or object?
Two situations:- pointer: setCentralWidget(edit);
- object: setCentralWidget(&edit);
Object is deleted when mainform is deleted by C++.
object pointed by pointer is deleted by Qt library when mainform is deleted? What about place address object, not pointer? -
@AndrzejB said in When may use QMainWindow instead of QDialog ?:
pointer: setCentralWidget(edit);
new
=> simple, safe.object: setCentralWidget(&edit);
Usually OK, because
edit
variable goes out of scope and so is de-parented fromQMainWindow
before that gets destroyed.I prefer
new
. Note that situation is no different forsetCentralWidget()
than any other time you place a widget on a widget, nothing special. -
@JonB said in When may use QMainWindow instead of QDialog ?:
Usually OK, because edit variable goes out of scope and so is de-parented from QMainWindow before that gets destroyed.
If QMainWindow is set with WA_DeleteOnClose it will clash for sure when the window is closed.