当前位置:网站首页>Day19QPushButton的使用2021-10-30
Day19QPushButton的使用2021-10-30
2022-06-21 16:58:00 【晓晓暮雨潇潇】
QPushButton的使用
以QPushButton为例,介绍实例化对象的几种方式,并结合常用的类方法展示他们之间的区别,介绍窗口部件的常用方法。除此之外,介绍了程序入口函数main的执行流程。关键信息在代码注释中已经全部写好,请看代码部分。
main代码
#include "widget.h"
#include <QLabel>//这个类不是自己新建的类。是QT定义的类,包含头文件之后,可以用其实例化对象
#include <QApplication>
int main(int argc, char *argv[])//main函数是程序入口,argc是命令行参数个数,argv是用来存储所有命令的命令行参数
{
QApplication a(argc, argv);//该类对象用于管理应用程序资源,任何Qt程序都要有一个来接收命令行参数
Widget w;
QLabel label(&w); //传入了父对象的指针,指明w是其父对象,一方面可以内存空间回收,另一方面可以产生依赖关系,使label显示在w上
label.setText("Day19,加油");
label.move(100,200);
w.show();//show函数默认是用顶层的方式弹出
return a.exec();//消息循环机制(阻塞),相当于是while(ture)
//让QAPlication进入事件循环,当Qt应用程序运行时可以接收产生的事件,例如单击和按下事件
}
.h代码
#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代码
#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();//在堆区开辟内存,如果不指定父对象,那么内存需要开发者回收
btn1->setText("按钮1");
btn1->setParent(this);//设置父对象,依赖关系,this只得是当前基类的对象
btn1->resize(100,50);//调整大小
btn1->move(100,0);
QPushButton *btn2=new QPushButton("按钮2",this);//重载的构造函数可以将名字和父对象作为参数
btn2->move(300,300);//移动按钮
btn2->resize(100,50);
QPushButton btn3("this");//在栈区开辟内存,系统管理内存空间,显示不出来的原因是构造函数结束,内存被回收了
btn3.show();
btn4.setText("按钮4");
btn4.show();//在栈区开辟内存,全局变量,会以show的顶层方式显示
btn5.setText("按钮5");//在栈区开辟内存,全局变量,设置父对象之后会显示在父对象中。
btn5.setParent(this);
resize(600,400);//设置窗口大小
this->setWindowTitle("Day19练习");//设置窗口标题
setFixedSize(600,400);//设置固定窗口大小
}
Widget::~Widget()
{
delete ui;
}
效果

边栏推荐
- 天天在都在谈的S3协议到底是什么?一文带你了解S3背后的故事
- PHP连接Mysql8.0报错:Illuminate\Database\QueryException
- ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection!
- 剑指 Offer 12. 矩阵中的路径
- Typescript的继承
- 信创环境下缓存服务Redis集群部署
- 高考后网上查询信息,注意防范没有 SSL证书的网站
- 会议聊天室--开发文档
- PHP输出函数
- Node输出方式
猜你喜欢

2022 high altitude installation, maintenance and removal work license question bank and simulated examination

Node的json解析

Node输出方式

AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"

怎么安装 Laravel

是关于Linux中一步建立yum源缓存问题

EtherCAT igh 'Fatal Sync Error'——0x002C,0x001A
![[technical management] assembly number and sword team](/img/80/4b39d98c7b51c6c50a4cd5f0a38370.jpg)
[technical management] assembly number and sword team

Some basic features of typescript

What is the S3 protocol that we are talking about every day? This article takes you to understand the story behind S3
随机推荐
Move Protocol Beta测试版稳定,临时决定奖池规模再扩大
180亿,苏州诞生一个超级母基金
论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》
剑指Offer 57. 二叉树的下一个节点
Differences between WCDMA and LTE
epoll+threadpool高并发网络IO模型的实现
现有需求同步其他数据库用户信息到我们系统他们主键id为string我们主键为Long
科普大佬说 | 如何打造自己的AI创造力?
Tcpserver enable multithreading
原码、补码、反码的关系
How typescript is constructed
2022 high altitude installation, maintenance and removal work license question bank and simulated examination
TypeScript的类型检查
TypeScript编译生成文件对比
字节流量生意经:变现趁早、缝钱袋子、All in卖货
Win32com operation Excel
Can I use line as a product term in the cable industry? Generally not used
Redis6.0 new features (Part 1)
Typescript compilation generation file comparison
I got a pay cut in disguise