Tips:
在使用控件的时候如果没有智能提示,可能是没有包含头文件
在运行时,报【invalid use of xxx】可能是没有包含相关头文件
如果出现中文乱码:设置编译器的编码格式为UTF-8


本节主要包含创建一个按钮控件、显示按钮、设置按钮的父窗口、设置按钮内容,重设按钮大小、设置窗口大小和移动按钮等功能。
//mywidget.cpp
#include "mywidget.h"
//包含头文件
#include "QPushButton"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
//按钮控件
QPushButton *btn= new QPushButton;
//显示按钮
btn->show(); //以顶层的方式显示
//设置父窗口,让按钮依赖于当前窗口上
btn->setParent(this); //this指向当前窗口
//设置btn内容
btn->setText("你好");
//创建第二个按钮
QPushButton *btn2 = new QPushButton("hello",this);
//重设按钮大小
btn2->resize(200,50);
//设置窗口尺寸
this->resize(600,400);
//移动按钮
btn->move(0,0);
btn2->move(150,0);
//固定窗口尺寸(设置后窗口不可收缩)
this->setFixedSize(600,400);
//设置窗口标题
this->setWindowTitle("这是窗口标题");
}
MyWidget::~MyWidget()
{
}