当前位置:网站首页>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

边栏推荐
- 【7. 高精度除法】
- 并查集dsu
- JS special effects in the construction of animated web pages
- EMC Radiation Emission rectification - principle Case Analysis
- ATM机模拟系统
- Database daily question - day 19: top ranked travelers
- [4. high precision addition]
- WinForm project console debugging mode
- [7. high precision division]
- Day14QProgressBar2021-10-17
猜你喜欢

Architecture and practice of vivo container cluster monitoring system

EMC整改小技巧

FPGA Xilinx 7 Series FPGA DDR3 hardware design rules

Neo4j 智能供应链应用源代码简析

rt_ Message queue of thread

【9. 子矩阵和】

Day14QProgressBar2021-10-17

EMC輻射發射整改-原理案例分析
![[9. submatrix sum]](/img/97/32f11e2f26a1f313c808fcc1cd27b3.png)
[9. submatrix sum]

On Monday, I asked the meaning of the | -leaf attribute?
随机推荐
2022 brazing test simulation 100 questions and answers
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
Matlab learning notes (5) import and export of MATLAB data
Introduction to excellent verilog/fpga open source project (XXVII) - small CPU
Starting from the classification of database, I understand the graph database
[8. One dimensional prefix and]
Zap grammar sugar
Asemi Schottky diode 1N5819 parameters, 1N5819 replacement, 1N5819 source
Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd
Ioerror: no translation files found for default language zh cn Solutions for
GoLand启动时闪退
Wechat applet film and television comment exchange platform system graduation design (3) background function
C1-qt idea of realizing simple calculator 2021.10.15
rt_ Message queue of thread
June25,2022 PMP Exam clearance manual-4
Relative references must start with either “/“, “./“, or “../“.
[5. high precision subtraction]
Using OKR for HR digital transformation
【4. 高精度加法】
xpm_memory_tdpram原语的完整使用实例