QT button operation (02)

QT's .pro file

QT       += core gui//拥有的模块

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

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

TRANSLATIONS += \
    test_01_zh_MO.ts

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

insert image description here
hot key

//命名规范:类名:首字母大写,单词之间首字母大写
//快捷键:注释:ctrl+/
//运行;ctrl+r
//编译 ctrl+b
//字体缩放ctrl+鼠标滚轮
//查找:ctrl+f
//整行移动:ctrl+shift+↑或者↓
//帮助文档:F1
//自动对齐:ctrl+i
//同名之间的.h和.cpp切换:F4

Code example
1.

#include "mywidget.h"
#include<QPushButton>//按钮的头文件
//命名规范:类名:首字母大写,单词之间首字母大写
//快捷键:注释:ctrl+/
//运行;ctrl+r
//编译 ctrl+b
//字体缩放ctrl+鼠标滚轮
//查找:ctrl+f
//整行移动:ctrl+shift+↑或者↓
//帮助文档:F1
//自动对齐:ctrl+i
//同名之间的.h和.cpp切换:F4


myWidget::myWidget(QWidget *parent)
    : QWidget(parent)//初始化列表
{
    
    
    //创建按钮
    QPushButton *btn=new QPushButton;
    //窗口对象默认不会显示,必须调用show方法来显示窗口
     //show以顶层方式弹出
    btn->show();
}

myWidget::~myWidget()
{
    
    
}


Operation diagram:
insert image description here
2.

#include "mywidget.h"
#include<QPushButton>//按钮的头文件
//命名规范:类名:首字母大写,单词之间首字母大写
//快捷键:注释:ctrl+/
//运行;ctrl+r
//编译 ctrl+b
//字体缩放ctrl+鼠标滚轮
//查找:ctrl+f
//整行移动:ctrl+shift+↑或者↓
//帮助文档:F1
//自动对齐:ctrl+i
//同名之间的.h和.cpp切换:F4


myWidget::myWidget(QWidget *parent)
    : QWidget(parent)//初始化列表
{
    
     
    QPushButton * btn=new QPushButton;
    //让btn对象依赖在mywidget窗口中
    btn->setParent(this);
    //显示文本
    btn->setText("123");
    //第二种方式设置窗口
    QPushButton * btn2=new QPushButton("456",this);
    //重置窗口大小
    resize(600,400);
    //移动第二个窗口
    btn2->move(100,100);
}

myWidget::~myWidget()
{
    
    
}


Operation diagram:
insert image description here
3.

#include "mywidget.h"
#include<QPushButton>//按钮的头文件
//命名规范:类名:首字母大写,单词之间首字母大写
//快捷键:注释:ctrl+/
//运行;ctrl+r
//编译 ctrl+b
//字体缩放ctrl+鼠标滚轮
//查找:ctrl+f
//整行移动:ctrl+shift+↑或者↓
//帮助文档:F1
//自动对齐:ctrl+i
//同名之间的.h和.cpp切换:F4


myWidget::myWidget(QWidget *parent)
    : QWidget(parent)//初始化列表
{
    
     
    QPushButton * btn=new QPushButton;
    //让btn对象依赖在mywidget窗口中
    btn->setParent(this);
    //显示文本
    btn->setText("123");
    //第二种方式设置窗口
    QPushButton * btn2=new QPushButton("456",this);
    //重置窗口大小
    resize(600,400);
    //移动第二个窗口
    btn2->move(100,100);
    //设置窗口标题
    setWindowTitle("第一");
    //设置固定窗口大小
    setFixedSize(600,400);
}

myWidget::~myWidget()
{
    
    
}


Guess you like

Origin blog.csdn.net/weixin_45866980/article/details/126860107