Window, child window, window geometry size

#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QWidget>
int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    MainWindow w;
//    w.show();
    //新建一个窗口 没有父对象
    QWidget *widgetParent = new QWidget();
  //QWidget *wiidgetParent = new QWidget(0,Qt::FramelessWindowHint); 
  //QWidget构造函数:QWidget::QWidget(QWidget * parent = 0, Qt::WindowFlags f = 0) 
    widgetParent->setWindowTitle(QObject::tr("我是widget窗口"));
    // labelChild指定了父窗口为widget,所以不是窗口
    QLabel *labelChild = new QLabel(widgetParent);
    labelChild->setText(QObject::tr("labelChild:我不是独立窗口,\n只是widget的子部件"));
 
    // 新建QLabel对象,没有父对象
    QLabel *labelParent = new QLabel();
    labelParent->setWindowTitle(QObject::tr("我是labelParent窗口"));
    //设置窗口的大小
    labelParent->resize(300,300);
    // 设置要显示的信息
    labelParent->setText(QObject::tr("labelParent:我是个窗口"));
 
 
    // 在屏幕上显示出来
    labelParent->show();
    widgetParent->resize(300,300);
    widgetParent->show();
 
 
    int ret = a.exec();
    delete widgetParent;//会自动删除labelChild;
    delete labelParent;
    return ret;
}

Widgets (Widgets) include QMainWindow, QWidget, QDialog, buttons, tables, radio buttons and other widgets.
Generally, we call widgets that are not embedded in other widgets as windows.
Windows generally have title bars and borders.

A window is a component without a parent component, so it is called a top-level component, and its opposite is a non-window component, also known as a child component

Insert picture description here
Examples of commonly used signs

Qt::FramelessWindowHint is used to generate a window without a frame.

Qt::WindowStaysOnTopHint is used to make the window stay on top of all other windows.

Qt::Dialog is used to generate a dialog window (that is, a window with a question mark)

Qt::SplashScreen is used to generate a window without a border

Widget: Abbreviated as Widget, it is the main component of building Qt interface. The widgets not only include the QMainWindow, QDialog, and QWidget we mentioned earlier, but also the buttons, labels, tables, radio buttons, etc. that we commonly use are all widgets.

Window: We call components that are not embedded in other components as windows. Windows generally have title bars and borders. A window is a component without a parent component, so it is called a top-level component.

Sub-components: also known as non-window components, most of the components in Qt are used as sub-components, they are embedded in other windows, such as buttons, labels, radio boxes, etc. we often say.

Window type
QWidget constructor: QWidget::QWidget(QWidget * parent = 0, Qt::WindowFlags f = 0)

(1) The previous parent refers to the parent widget, and the default value is 0, indicating that there is no parent window;

(2) The f parameter is of the Qt::WindowFlags enumeration type, divided into window type (WindowType) and window flags (WindowFlags).

(3) The former can define the type of window. For example, where f=0, it means that Qt::Widget is used. This is the default type of QWidget. If this type of widget has a parent window, then it is a child widget. Otherwise, it is an independent window.

(4) The latter includes many types,

Examples of commonly used signs

Qt::FramelessWindowHint is used to generate a window without a frame.

Qt::WindowStaysOnTopHint is used to make the window stay on top of all other windows.

Qt::Dialog is used to generate a dialog window (that is, a window with a question mark)

Qt::SplashScreen is used to generate a window without a border.

The geometric size
of the window For a window, it is often necessary to set its size and position at runtime. This is the geometric layout of the window. As you have seen in the previous example, the default size of widgetParent is the size of the child part labe2Child it contains. Even if widgetParent sets the size, it is subject to labe2Child.

The position of widgetParent and labe2Child on the window when they appear is also uncertain. For the size and position of the window, different functions are used to obtain it according to whether it contains a border and a title bar. You can view the Window and Dialog Widgets keywords in the help index. The geometric layout of the window is shown in the document.

The functions here are divided into two categories, one category contains frames, and the other category does not contain frames:

Contains frames: functions such as x(), y(), frameGeometry(), pos() and move();

Does not include frames: functions such as geometry(), width(), height(), rect() and size().

You can add the following code to get:

qDebug()<<"widgetParent->x();"<<widgetParent->x();
qDebug()<<"widgetParent->y();"<<widgetParent->y();
qDebug()<<"widgetParent->pos();"<<widgetParent->pos();
qDebug()<<"widgetParent->frameGeometry();"<<widgetParent->frameGeometry();
qDebug()<<"widgetParent->geometry();"<<widgetParent->geometry();
qDebug()<<"widgetParent->width();"<<widgetParent->width();
qDebug()<<"widgetParent->height();"<<widgetParent->height();
qDebug()<<"widgetParent->size();"<<widgetParent->size();
qDebug()<<"widgetParent->rect();"<<widgetParent->rect();

//把主窗体几何位置给子窗体
 child->setGeometry(this->geometry());
 

Guess you like

Origin blog.csdn.net/weixin_44972129/article/details/109748349