QT之QPushButton

在使用之前需要添加 #include "QPushButton"

 

 下面是项目结构截图

 

 

代码:

复制代码
#include "mainwindow.h"
#include "QPushButton"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QPushButton *button1=new QPushButton;
    button1->setText("第一个按钮");
    //让这个对象依赖于当前窗口对象,其实这时候就相当于指定了父对象,具体意义可以查看对象树
    button1->setParent(this);
    //设置按钮大小
    button1->setFixedSize(60,40);

    QPushButton *btn2=new QPushButton("第二个按钮",this);
    //移动位置
    btn2->move(100,100);
    //设置窗体固定大小,并且不允许拖动改变窗体大小
    this->setFixedSize(600,400);
    //设置界面大小
    this->resize(600,400);
    //设置窗体标题
    this->setWindowTitle("第一个窗体");
}

MainWindow::~MainWindow()
{
}
复制代码

 

结果:

 

我们创建的这个QObject对象会自动添加到其父对象的children()列表

posted @   安静点--  阅读(233)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示