Qt按钮控件常用的API

1.创建按钮

QPushButton *btn=new QPushButton;

以顶层方式弹出窗口控件

代码:

复制代码
#include "widget.h"
#include "ui_widget.h"
#include"QPushButton"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建一个按钮
    QPushButton *btn=new QPushButton;
    btn->show();//show以顶层方式弹出窗口控件
}

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

2.按钮依赖在widget窗口中

复制代码
btn->setParent(this);

//显示文本
btn->setText("第一个按钮");

3. 重置窗口大小

复制代码
//重置窗口大小
 resize(600,400);

按钮也可以重新制定大小

but2->resize(50,50);

4.后创建的按钮会把先创建的覆盖

复制代码
#include "widget.h"
#include "ui_widget.h"
#include"QPushButton"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建一个按钮
    QPushButton *btn=new QPushButton;
    //btn->show();//show以顶层方式弹出窗口控件
    //让btn对象,依赖在Widget窗口中
    btn->setParent(this);

    //显示文本
     btn->setText("第一个按钮....");

    //创建第二个按钮
    QPushButton *btn2=new QPushButton("第二个按钮",this);

     //重置窗口大小
     resize(600,400);
}

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

此时btn2会把btn1覆盖

可以增加一行代码,将btn2移动一下

复制代码
btn2->move(100,100);

运行结果:

5.调整窗口

复制代码
//设置固定的窗口大小
     setFixedSize(600,400);

//设置窗口标题
     setWindowTitle("第一个窗口");

此时窗口的大小不可调,窗口的名字也修改了

相关推荐
用户805533698033 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner3 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz8 天前
QML Hello World 入门示例
qt
xcyxiner11 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner12 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner12 天前
DicomViewer (添加模型类)3
qt
xcyxiner13 天前
DicomViewer (目录调整) 2
qt
xcyxiner13 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能15 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G15 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt