1. QWidget类
QWidget类是Qt所有图形用户界面(组件)的基类,因此QWidget类内部规定了所有最基础的UI相关功能。
例如以下成员:
● width : const int 宽度(单位:像素,后文同)
Qt中的getter和setter属性都在Access functions下面
● height : const int 高度
// 修改宽高可以使用此函数
// 参数1:宽度
// 参数2:高度
void resize(int w, int h)
● x : const int
横坐标,在计算机中原点在左上角,x轴正方向向右
● y : const int
纵坐标,y轴正方向向下
// 移动坐标,以组件的左上角为准,包括边框
// 参数1:横坐标
// 参数2:纵坐标
void move(int x, int y)
// 同时设置宽高与坐标
// 参数1:横坐标
// 参数2:纵坐标
// 参数3:宽度
// 参数4:高度
void QWidget::setGeometry(int x, int y, int w, int h)
2. 子组件
之前的窗口都是内容为空的,实际上一个窗口内部必然会有组件对象(常见如按钮、图片等),本节以按钮类QPushButton为例,讲解如何显示到窗口中。
// QPushButton的构造函数
// 参数1:按钮显示的文字,QString是Qt的字符串类型,后续讲
// 参数2:父组件对象
QPushButton::QPushButton(const QString & text,
QWidget * parent = 0)
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
// 头文件
#include <QDebug>
#include <QPushButton> // 按钮类
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton* btn; // 成员变量!
};
#endif // DIALOG_H
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
// 默认编译器会使用this(即主函数的对象w)
this->resize(400,800);
this->move(100,100);
// 创建一个按钮对象(堆内存!)
btn = new QPushButton("你好",this);
// 设置宽高位置
btn->setGeometry(100,100,100,100);
}
Dialog::~Dialog()
{
delete btn;
qDebug() << "析构函数";
}
记录
按钮组件记录:
标签:Dialog,入门,int,QPushButton,UI,QWidget,100,btn From: https://blog.51cto.com/u_14458591/8095379