QT——入门之常用控件属性设置

这里我在这讲一下,我在开发项目的时候常用的使用方式。QT的功能很强大,构造函数的方式也很多。以下方式可供初学者参考。后续会继续更新!

QLabel 标签

//需要添加头文件
#include <QLabel>       //标签

//常见属性设置
//建立对象
this->InterfaceTitle = new QLabel(this);

//设置标签的位置,注意我传入的是QRect
InterfaceTitle->setGeometry(QRect(154, 63, 170, 15));

//设置标签文字
InterfaceTitle->setText("设置标签里面的文字");

//设置文字的排版方式为水平居中
InterfaceTitle->setAlignment(Qt::AlignCenter);
/*
(1)Qt::AlignLeft::水平方向靠左。
(2)Qt::AlignRight:水平方向靠右。
(3)Qt::AlignHCenter:水平方向居中。
(4)Qt::AlignJustify:水平方向调整间距两端对齐。
(5)Qt::AlignTop:垂直方向靠上。
(6)Qt::AlignButton:垂直方向靠下。
(7)Qt::AlignVCenter:垂直方向居中。
(8)Qt::AlignCenter:水平、垂直方向居中
*/

//设置图片为标签的背景括号里面传入Pixmap对象
//InterfaceTitle->setPixmap(pixmap_GameLogo);
//可以使用Pixmap对象的scaled来设置图片大小
//pixmap_GameLogo = pixmap_GameLogo.scaled(20,20);
	
InterfaceTitle->setStyleSheet(QString::fromUtf8("QLabel{font: bold;\n"
"font-size:15px; color: yellow;background-color: green; }");	
 //设置标签属性,
 /*
 font: bold  :字体加粗
 font-size:15px :字体大小为15
 color: yellow:字体颜色为黄色
 background-color: green:标签背景设置为绿色 
 */

补充:上面的代码中有用到QString::fromUtf8,举例一下:

QString A = QString::fromUtf8(“括号里面的东西太长的的话可以这样\n”

“接下去写自己想写的东西,但是末尾要用\n记得双引号也要再括起来”);

QPushButton 按钮

//需要的头文件
#include <QPushButton>

//设置位置、大小
my_pushButton = new QPushButton(this);
my_pushButton->setGeometry(QRect(150, 290, 150, 45));

//设置图片为按钮背景  
QPixmap pixmap_MusicButto("image/btn/login.png");
my_pushButton->setIcon(pixmap_MusicButton);
my_pushButton->setIconSize(QSize(150, 45));

//除去图片透明的部分
my_pushButton->setStyleSheet("QPushButton{background-color:transparent;}");

QLineEdit 编辑框

//需要的头文件
#include <QLineEdit> 

//创建对象,并且设置位置
this->pwdLineEdit = new QLineEdit(this);
pwdLineEdit->setGeometry(QRect(170, 310, 165, 31));
//限制编辑框里字符长度
pwdLineEdit->setMaxLength(8);
//设置属性
PortLineEdit->setStyleSheet("QLineEdit{font-size:15px; color: yellow;}");
//设置成密文,
pwdLineEdit->setEchoMode(QLineEdit::Password);

其中setEchoMode(QLineEdit::Password);还可以是一下其中一种
1、QLineEdit::Normal 显示输入编辑框内的字符
2、QLineEdit::NoEcho 不显示任何内容
3、QLineEdit::Password 显示星号,暗文显示
4、QLineEdit::PasswordEchoOnEdit 在编辑时显示输入的字符,否则显示星号

以上就是我总结的常用控件,后续我也会继续更新其中的内容,其中QLineEdit 编辑框会常常用到正则表达式,由于篇幅过大,我会在下篇讲解。

猜你喜欢

转载自blog.csdn.net/l1206715877/article/details/106173908