QT学习2(QPushButton创建,对象树)

 

目录

1.QPushButton创建

1.1 简介

1.2实操

 1.3 总结

2.对象树

3.Qt窗口体系 

1.QPushButton创建

1.1 简介

QPushButton属于QPushButton类,使用这个类要包含头文件#include<QPushButton>,其父类为QAbstractButton类,而QAbstractButton类的父类是Qwidget类

1.2实操

​​​​​​在widget。cpp下的构造函数进行操作

#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //创建一个按钮
    QPushButton *nt=new QPushButton;
    //show是Qwidget的成员函数nt从爷爷辈继承了下来
    nt->show();

}

Widget::~Widget()
{
}

此时显示的按钮并没出现在untitle窗口,这是因为show以顶层的方式弹出窗口控件

如果要让对象出现在untitle窗口,需要设置nt父类与show为同一父类,有一个函数,setparent可以实现

#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //创建一个按钮
    QPushButton *nt=new QPushButton;
    //show是Qwidget的成员函数nt从爷爷辈继承了下来
    //nt->show();
    nt->setParent(this);
}

Widget::~Widget()
{
}

 要对按钮上加子用settext

#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //创建一个按钮
    QPushButton *nt=new QPushButton;
    //show是Qwidget的成员函数nt从爷爷辈继承了下来
        //nt->show();
    nt->setParent(this);
    nt->setText("first");
}

Widget::~Widget()
{
}

 创建按钮还有另一种方法,直接在初始化的时候便可以

#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
 
    QPushButton *nt2=new QPushButton("second",this);
}

Widget::~Widget()
{
}

 可以用move函数移动按钮

#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
 
    QPushButton *nt2=new QPushButton("second",this);
    nt2->move(100,100);
}

Widget::~Widget()
{
}

 如果设置了两个按钮就需移动,否则会覆盖

 resize函数可定义边框大小

setFixedSize函数使边框大小固定

setWindowTitle函数用于指定名称,具体看下
#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //创建一个按钮
    QPushButton *nt=new QPushButton;
    //show是Qwidget的成员函数nt从爷爷辈继承了下来
        //nt->show();
    nt->setParent(this);
    nt->setText("first");
    QPushButton *nt2=new QPushButton("second",this);
    nt2->move(100,100);
//运行时边框大小
    resize(600,400);
//改边框名为first
    setWindowTitle("FIRST");
//是边框大小固定
    setFixedSize(600,400);
}

Widget::~Widget()
{
}

按钮指定大小(resize)

#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //创建一个按钮
    QPushButton *nt=new QPushButton;
    //show是Qwidget的成员函数nt从爷爷辈继承了下来
        //nt->show();
    nt->setParent(this);
    nt->setText("first");
    QPushButton *nt2=new QPushButton("second",this);
    nt2->move(100,100);
    resize(600,400);
    setWindowTitle("FIRST");
    setFixedSize(600,400);
    nt2->resize(50,50);
}

Widget::~Widget()
{
}

 

 1.3 总结

创建QPushButton * btn = new QpushButton

设置父亲setParent(this)

设置文本setText(“文字"")

设置位置move(宽,高)

重新指定窗口,按钮大小resize

设置窗口标题setWindowTitler

设置窗口固定大小setFixedsize

打印信息引头文件QDebug,打印方式:qDebug<<“   ”<<endl;

2.对象树

向下构造,向上析构,析构时会从上检索,如果发现子类,先析构子类

 当创建的对象在堆区时候,如果指定的父亲是aobject派生下来的类或者Qobject子类派生下来的类,可以不用管理释放的操作,将对象会放入到对象树中。一定程度上简化了内存回收机制

3.Qt窗口体系 

坐标体系

 

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shany-Ming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值