The Qwidget class is the base class for all user-interface objects, called the Base window part.
#include <QApplication> #include <QLabel> #include <qwidget>int main (int argc, char *argv[]) { Qapplication A (argc, argv); New Qwidget class object, the default parent parameter is 0, so it is a window qwidget *widget = new Qwidget (); Set the window caption widget->setwindowtitle (qobject::tr ("I Am the Widget")); New Qlabel object, the default parent parameter is 0, so it is a window qlabel *label = new Qlabel (); Label->setwindowtitle (Qobject::tr ("I am a label")); Set the information to be displayed Label->settext (qobject::tr ("Label: I am a Window")); Change the size of the part so that it can show the complete content label->resize (+); LABEL2 specifies that the parent window is a widget, so it is not a window qlabel *label2 = new Qlabel (widget); Label2->settext (Qobject::tr ("Label2: I'm not a standalone window, just a widget's subassembly"); Label2->resize (+); Display on the screen label->show (); Widget->show (); return a.exec ();}
1. In QT, parts that are not embedded in other parts are called Windows, and the general window has borders and title bars, just like widgets and labels in a program. A window is a part that has no parent part, and all is called a top-level part. Its relative non-window part, also known as a child part.
2. There are drawbacks to this simple statement, such as: Qdialog is a window is beyond doubt, but in the use of dialog boxes for which the parent window is also normal;
3. I read the last two lines of code delete label; Delete widget, but after adding these two sentences, the result will be displayed in the background and will not appear on the desktop;
4. In the program, the label does not have a parent window, and Label2 in the widget, the widget is its parent window. The program uses new to allocate space for the Label2, but does not use Delete to release it because the child objects are automatically destroyed when the parent object is destroyed in Qt.
QT Window widgets and sub-parts