【Qt】1.main、QPushButton、对象树

目录

新建项目说明

main.cpp

pro文件

mywidget.h

mywidget.cpp

QPushButton

mywidget.cpp

对象树

新建项目说明

main.cpp

#include "mywidget.h"
#include <QApplication>//包含头文件 应用程序

int main(int argc, char *argv[])
{
    //应用程序对象 a,Qt中有且仅有一个应用程序对象
    QApplication a(argc, argv);

    //创建MyWidget对象w,MyWidget基类是QWidget
    MyWidget w;

    //窗口默认是不会弹出的,如果想弹出,调用show()
    w.show();

    //a.exec()进入消息循环机制pause
    return a.exec();
}

pro文件

QT       += core gui	//包含的模块

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets		//大于4版本,包含widgets模块

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mywidget.cpp		//源文件

HEADERS += \
    mywidget.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin 
!isEmpty(target.path): INSTALLS += target

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class MyWidget : public QWidget     //MyWidget继承QWidget
{
    Q_OBJECT    //宏定义,写了这个宏就支持了Qt中的信号和槽机制

public:
    MyWidget(QWidget *parent = nullptr);        //构造函数
    ~MyWidget();        //析构函数
};
#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"

/*
 * 命名规范
 * 类名:首字母大写,单词与单词之间首字母大写
 * 函数、变量:首字母小写, 单词与单词之间首字母大写
 *
 * 快捷键
 * 运行:Ctrl+r
 * 编译:Ctrl+b
 * 查找:Ctrl+f
 * 帮助文档:F1
 * 自体缩放:Ctrl+滚轮
 * 自动对齐:Ctrl+i
 * 同名之间的.h .cpp切换:F4
 */

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)   //初始化列表
{
}

MyWidget::~MyWidget()
{
}

QPushButton

  1. QPushButton *btn=new QPushButton;

  2. 设置父亲:btn->setParent(this);

  3. 设置文字:btn->setText("你好,世界!");

  4. 移动按钮:btn2->move(100,100);

  5. 重置窗口大小:resize(600,400);

  6. 重置窗口标题:setWindowTitle("Qt第一个窗口");

  7. 设置固定窗口大小:setFixedSize(600,400);

mywidget.cpp

#include "mywidget.h"
#include <QPushButton>

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)   //初始化列表
{
    //按钮
    QPushButton *btn=new QPushButton;
    //btn->show();//show用顶层方式弹出,在MyWidget窗口中显示,需要依赖MyWidget窗口

    //设置父亲
    btn->setParent(this);

    //设置文字
    btn->setText("你好,世界!");//将char *隐式类型转为Qstring

    //创建按钮第二种方式,窗口会按照btn2大小进行显示
    QPushButton *btn2=new QPushButton("第二按钮",this);

    //重置窗口大小
    this->resize(600,400);

    //移动第二个按钮
    btn2->move(100,100);

    //按钮重置大小
    btn2->resize(50,50);

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

    //设置固定窗口大小
    setFixedSize(600,400);
}

MyWidget::~MyWidget()
{
}

对象树

  1. 所有new出来的对象,不用管释放

  2. 原因children表中的对象会在窗口关闭后进行自动释放

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

因心,三人水

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

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

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

打赏作者

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

抵扣说明:

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

余额充值