Qt入门1---widget、mainwindow和Dialog区别

时间:2023-03-09 19:30:43
Qt入门1---widget、mainwindow和Dialog区别

摘要: 看了一个月的Qt,居然没有理清Qt中

------------------------------------

1.QMainWindow

A main window provides a framework for building an application's user interface. Qt has QMainWindow and its related classes for main window management. QMainWindow has its own layout to which you can addQToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.

Qt入门1---widget、mainwindow和Dialog区别

2.QDialog

The QDialog class is the base class of dialog windows.

A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide a return value, and they can have default buttons. QDialogs can also have a QSizeGrip in their lower-right corner, using setSizeGripEnabled().

Note that QDialog (and any other widget that has type Qt::Dialog) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar entry.

重要的看.............

QDialog继承自QWidget。
那么,看一下QDialog都新增了哪些功能。如果你需要这些新增的功能,那么考虑使用QDialog。
Qt文档中有这样一段介绍文字:
A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide a return value, and they can have default buttons. 
从这段话可以看出,QDialog通常作为一个顶层窗口出现,允许用户进行一些选择,并返回用户选择的结果(比如:在一个配置对话框里,用户操作完后是按下了“确定”、“应用”或“取消”)。然后应用程序就可以根据用户刚才的选择决定下一步做什么(比如:如果用户刚才按下了“确定”,那么就保存用户在配置对话框中进行的更改等)。

由于QDialog通常作为顶层窗口,所以在使用诸如QStackedLayout这样的布局类时,一般上使用QWidget这样“原始”的窗体,然后把它添加到Stacked Layout中作为其中的一个Page。

QWidget中有个方法叫SetLayout,QDialog继承了它。