当前位置:网站首页>Use of day19qpushbutton 2021-10-30

Use of day19qpushbutton 2021-10-30

2022-06-22 02:47:00 Morning and evening rain

QPushButton Use

With QPushButton For example , Introduce several ways to instantiate objects , And show the differences between them in combination with common class methods , Introduce the common methods of window components . besides , The program entry function is introduced main The implementation process of . The key information has been written in the code comments , See the code section .

main Code

#include "widget.h"
#include <QLabel>// This class is not a newly created class . yes QT Defined classes , After including the header file , You can instantiate objects with them 
#include <QApplication>

int main(int argc, char *argv[])//main A function is a program entry ,argc Is the number of command line arguments ,argv Is the command line parameter used to store all commands 
{
    
    QApplication a(argc, argv);// This class of objects is used to manage application resources , whatever Qt Every program should have a to receive command line parameters 
    Widget w;
    QLabel label(&w);  // Passed in a pointer to the parent object , To specify w Is its parent , On the one hand, it can reclaim memory space , On the other hand, dependencies can be generated , send label Displayed in the w On 
    label.setText("Day19, come on. ");
    label.move(100,200);
    w.show();//show By default, the function pops up in the top-level mode 
    return a.exec();// Message loop mechanism ( Blocking ), Equivalent to while(ture)
                    // Give Way QAPlication Enter event loop , When Qt The application runtime can receive the generated events , For example, click and press events 
}

.h Code

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui {
     class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
    
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QPushButton btn4;
    QPushButton btn5;
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

.cpp Code

#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    
    ui->setupUi(this);
    QPushButton *btn1 = new QPushButton();// Open up memory in the heap , If you do not specify a parent object , Then the memory needs to be reclaimed by the developer 
    btn1->setText(" Button 1");
    btn1->setParent(this);// Set parent , Dependency relationship ,this It must be the object of the current base class 
    btn1->resize(100,50);// Resize 
    btn1->move(100,0);


    QPushButton *btn2=new QPushButton(" Button 2",this);// Overloaded constructors can take names and parent objects as arguments 
    btn2->move(300,300);// Move button 
    btn2->resize(100,50);

    QPushButton btn3("this");// Open up memory in the stack area , System management memory space , The reason it doesn't show is that the constructor ends , Memory is recycled 
    btn3.show();

    btn4.setText(" Button 4");
    btn4.show();// Open up memory in the stack area , Global variables , Will show Top level display of 

    btn5.setText(" Button 5");// Open up memory in the stack area , Global variables , After setting the parent object, it will be displayed in the parent object .
    btn5.setParent(this);

    resize(600,400);// Set window size 
    this->setWindowTitle("Day19 practice ");// Set the window title 
    setFixedSize(600,400);// Set fixed window size 
}

Widget::~Widget()
{
    
    delete ui;
}

effect

 Insert picture description here

原网站

版权声明
本文为[Morning and evening rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211658129876.html