ChatGPT解决这个技术问题 Extra ChatGPT

What's the difference between QMainWindow, QWidget and QDialog?

What's the difference between QMainWindow, QWidget and QDialog?


P
Peter Tseng

A QWidget is the base class for all drawable classes in Qt. Any QWidget-based class can be shown as a window by showing it when it has no parent.

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. It has predefined places for a menu bar, a status bar, a toolbar, and other widgets. It does not have any built-in allowances for buttons like QDialog does.


I searched for this question but that's not really the answer I was waiting for. In QtDesigner using a QMainWindow gives you a menu bar and a status bar. When you write the code yourself and use QMainWindow you don't have this things. Or your saying that you can do something like window.addToolBar("Toolbar"); and it will place it at a predefined place without any other headache?
@MoraruLilian: I don't actually use QMainWindow, so I don't know the particulars. As I said in my answer, I know it has places for them, so doing it in code might be as simple as you state.
@Caleb_Huitt It does what I am saying. I wanted just clear up if that's what you are trying to say. Anyway, I would recommend always using QWidget instead of QMainWindow. You can get into problems, for example with setting another Layout. If you want to set a new layout in QMainWindow you would have to create a QWidget and set it as a central widget for QMainWin and just then in QWidget you could setLayout(QLayout *). While you could avoid additional headaches just by using QWidget. Sure, QMainWindow offers that easy setup of the ToolBar and StatusBar but you can do that yourself in QWidget...
One reason we are looking to use QMainWindow instead of just the base class QWidget is that QMainWindow has QDockWidget support whereas QWidget alone does not
@mLstudent33 That sounds like a good question for documentation, and if that doesn't help, for asking here as a primary question rather than in the comments. One example (for Qt5) can be seen at doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html
L
Lalit kumar

QWidget :is a base class for all other GUI elements in QtWidgets module. It can constitute a window by itself, or be part of a QLayout, or just a member of parent-child hierarchy.

QDialog :is usually used to - surprise! - display a temporary dialog when user input is required.

QMainWindow :is a convenience class that can be used as the main window of your application. It has some nice features built-in: a status bar, tool bars and a menu bar


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now