目录
新建项目说明
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
QPushButton *btn=new QPushButton;
设置父亲:btn->setParent(this);
设置文字:btn->setText("你好,世界!");
移动按钮:btn2->move(100,100);
重置窗口大小:resize(600,400);
重置窗口标题:setWindowTitle("Qt第一个窗口");
设置固定窗口大小: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()
{
}
对象树
所有new出来的对象,不用管释放
原因children表中的对象会在窗口关闭后进行自动释放
893

被折叠的 条评论
为什么被折叠?



