QT learning -- coordinate system and memory recovery mechanism

Coordinate system:

1. For the main window, the coordinate system is relative to the screen

Origin: relative to the upper left corner of the screen

x: Increment right

y: Increment down

 

2. For a child window, the coordinate system is relative to the main window

Origin: relative to the upper left corner of the blank area of the window (excluding the border)

 

Coord.cpp Document code:

#include "coord.h"
#include<QPushButton>
#include"mybutton.h"
Coord::Coord(QWidget *parent)
    : QWidget(parent)
{
    /*
     *For the main window, the coordinate system is relative to the screen
     * Origin: relative to the upper left corner of the screen
     *x:Increment right
     *y: Increment down
     */
    move(100,100);//Move main window
    resize(300,400);
    /*
     * For child windows, the coordinate system is relative to the main window
     * Origin: relative to the upper left corner of the blank area of the window (excluding the border)
     */
    QPushButton*w=new QPushButton(this);
    w->move(0,0);
    w->setText("HAHA");
    w->resize(200,100);//Set button size


    //Set a button in the button
    /*
     * It can be understood that for any two windows, including a button, it can also be regarded as a window,
     * There are main and secondary windows. The movement of the parent window is always based on the upper left corner of the main window
     */
    QPushButton*p=new QPushButton(w);
    p->move(0,0);
    p->setText("I am the sub button of the button");
}

 

Program operation:

 

To sum up, we can see:

I set another button in the button, and found that for any two windows, including a button, it can also be regarded as a window. There are main and secondary windows, and the movement of the parent window always takes the upper left corner of the main window as the reference

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

Memory recovery mechanism:

In order to understand the QT framework memory recycling mechanism of C + +, the QPushButton class is inherited again to form a MyButton class.

MyButton.h file code:

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QPushButton>
#include<QWidget>
class MyButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyButton(QWidget *parent = nullptr);//The QWidget here doesn't change because I need to set the main window of the button to the largest base window
    ~MyButton();

signals:

};

#endif // MYBUTTON_H

MyButton.cpp Document code:

#include "mybutton.h"
#include<QPushButton>
#include<QDebug>
#include<QWidget>
MyButton::MyButton(QWidget *parent) : QPushButton(parent)//Call the constructor of the original button to specify the base class window as the parent class window of the button
{

}
MyButton::~MyButton()
{
    qDebug()<<"Button destructed";//Print this sentence when the destructor is called
}

Coord.cpp Document code:

#include "coord.h"
#include<QPushButton>
#include"mybutton.h"
Coord::Coord(QWidget *parent)
    : QWidget(parent)
{
    /*
     *For the main window, the coordinate system is relative to the screen
     * Origin: relative to the upper left corner of the screen
     *x:Increment right
     *y: Increment down
     */
    move(100,100);//Move main window
    resize(300,400);
    /*
     * For child windows, the coordinate system is relative to the main window
     * Origin: relative to the upper left corner of the blank area of the window (excluding the border)
     */
    QPushButton*w=new QPushButton(this);
    w->move(0,0);
    w->setText("HAHA");
    w->resize(200,100);//Set button size


    //Set a button in the button
    /*
     * It can be understood that for any two windows, including a button, it can also be regarded as a window,
     * There are main and secondary windows. The movement of the parent window is always based on the upper left corner of the main window
     */
    QPushButton*p=new QPushButton(w);
    p->move(0,0);
    p->setText("I am the sub button of the button");



    MyButton*i=new MyButton(this);
    i->move(200,200);
    i->setText("MyButton");
    //According to the program operation

    //1. After specifying the parent
    //2. Directly or indirectly inherited from QObject
    //3. If the sub object is allocated dynamically, the new function is used
    //Then sub objects do not need to be released manually, i.e. delete is used, and the system will release them automatically


}

Program operation:
 

When I click "x" in the upper right corner to end the program

Printed out this sentence, that is, the space out of the new, after the program runs automatically released.

 

 

To sum up,

Space satisfying the following three conditions:

1. After specifying the parent

2. Directly or indirectly inherited from QObject

3. If the sub object is allocated dynamically, the new function is used

Then sub objects do not need to be released manually, i.e. delete is used, and the system will release them automatically

Keywords: Windows Qt

Added by Volatile on Thu, 18 Jun 2020 14:06:11 +0300