明港网站建设公司,引流推广话术文案,全屏网站代码,百度云 wordpress 教程视频1. QWidget类
QWidget类是Qt所有图形用户界面#xff08;组件#xff09;的基类#xff0c;因此QWidget类内部规定了所有最基础的UI相关功能。例如以下成员#xff1a; ● width : const int 宽度#xff08;单位#xff1a;像素#xff0c;后文同#xff09; Qt中的…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_OBJECTpublic:Dialog(QWidget *parent 0);~Dialog();private:QPushButton* btn; // 成员变量
};#endif // DIALOG_H#include dialog.hDialog::Dialog(QWidget *parent): QDialog(parent)
{// 默认编译器会使用this即主函数的对象wthis-resize(400,800);this-move(100,100);// 创建一个按钮对象堆内存btn new QPushButton(你好,this);// 设置宽高位置btn-setGeometry(100,100,100,100);
}Dialog::~Dialog()
{delete btn;qDebug() 析构函数;
}记录 按钮组件记录 btn要设置为栈对象让其在程序运行期间一直存在而不是一闪而过。在析构函数Dialog()中要进行销毁所以要在Dialog类中对该对象进行私有化的声名以便在析构函数Dialog()中可以调用到。
3. 样式表
Qt允许给组件设置自定义样式通过QWidget的一个属性实现styleSheet : QString 这个属性的值是一个QSS/CSS语法的字符串可以指定组件的样式。 设置样式表离不开颜色值在计算机中颜色是通过红绿蓝三种色彩叠加而成每种色彩是8位的深度即0-255分别表示从暗到亮例如(255,0,0)表示正红色255,255,255表示纯白色…十六进制也是常用的表示方式需要加#作为前缀更多色彩可参考 在线颜色选择器 | RGB颜色查询对照表 Color Palette Generator - Create Beautiful Color Schemes
#ifndef DIALOG_H
#define DIALOG_H#include QDialog
// 头文件
#include QDebug
#include QPushButton // 按钮类#define QPushButton_STYTLE (QString(\
/*按钮普通态*/\
QPushButton\
{\font-family:Microsoft Yahei;\/*字体大小为20点*/\font-size:20pt;\/*字体颜色为白色*/\color:white;\/*背景颜色*/\background-color:rgb(14 , 150 , 254);\/*边框圆角半径为8像素*/\border-radius:8px;\
}\
/*按钮悬停态*/\
QPushButton:hover\
{\/*背景颜色*/\background-color:#9155a7;\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\/*背景颜色*/\background-color:rgb(14 , 135 , 10);\/*左内边距为3像素让按下时字向右移动3像素*/\padding-left:3px;\/*上内边距为3像素让按下时字向下移动3像素*/\padding-top:3px;\
}))class Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent 0);~Dialog();private:QPushButton* btn; // 成员变量
};#endif // DIALOG_H#include dialog.hDialog::Dialog(QWidget *parent): QDialog(parent)
{btn new QPushButton(你好,this);btn-setGeometry(100,100,100,100);btn-setStyleSheet(QPushButton_STYTLE);
}Dialog::~Dialog()
{delete btn;
}