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

边栏推荐
- Penetration testing - logic vulnerability topic
- import和require在浏览器和node环境下的实现差异
- The brand, products and services are working together. What will Dongfeng Nissan do next?
- June25,2022 PMP Exam clearance manual-5
- [5. high precision subtraction]
- 【Docekr学习遇到的坑】
- 如何选择合适的 Neo4j 版本(2022版)
- rt_ Message queue of thread
- 华阳智能冲刺深交所:拟募资4亿 复星惟盈是股东
- GraphConnect 2022 大会的产品发布一览
猜你喜欢
![[4. high precision addition]](/img/8c/1d07597b5ff3a573b453ac1ca19a5c.png)
[4. high precision addition]

Select for i/0 multiplexing

Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga

Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder

微软 IE 浏览器于 6 月 15 日被永久关闭

EMC rectification tips

【2. 归并排序】

In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened

ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment

Annual special analysis of China Mobile Banking in 2022
随机推荐
Implementation differences between import and require in browser and node environments
2022年买理财产品买三个月还是半年?
Markdown is proficient in Elementary Grammar and is compatible with marktext
mocklog_ Simulation log
Wechat applet film and television comment exchange platform system graduation design (2) applet function
Create RT_ Thread thread
How to apply PMP project management knowledge?
Microsoft Internet Explorer was permanently closed on June 15
Day13QMainWindow2021-09-28
Deep Copy
关于PMP考试,你想知道的知识都在这里了
Relative references must start with either “/“, “./“, or “../“.
2022 brazing test simulation 100 questions and answers
最热门海量的阿里云盘资源分享
[2. merge sort]
Global exception handling
Asemi fast recovery diode FR107 parameter, FR107 real object, FR107 application
Unity3d post process volume profile
Get to know unity3d (project structure, third-party plug-in of probuilder)
C ++ Primer 第2章 变量和基本类型 总结