Qt之QLabel类的应用

在Qt中,我们不可避免的会用到QLabel类。而Qlabel的强大功能作为程序员的你有多少了解?

下面,跟着我一起在来学习一下吧!

1、添加文本

Qlabel类添加文本有两种方式,一种是直接在实现时添加,如:

1 QLabel *label = new QLabel(QString("Holle,世界"), this);
2     //QLabel *label = new QLabel(tr("Holle,世界"), this);

一种是在实现后添加,如:

1 int a = 1+1;
2 QLabel *label = new QLabel( this);
3 label ->setText(tr("Holle,世界"));
4 //label ->setText(tr("1+1=%1").arg(a));
5 //label ->setText(QString::number(a));
6 //label ->setText(QString::number(a,'f',2));//保留两位,如果保留一位就把2改为1

2、设置尺寸,位置

设置尺寸也有多种,常用的固定尺寸(FixedSize),最小尺寸(MinimumSize),最大尺寸(MaximumSize),代码如下:

1 //setMinimumHeight(30);        //最小行高
2 //setMinimumWidth(30);        //最小列宽
3 setMinimumSize(370, 150);    //设置最小尺寸
1 //setMaximumHeight(30);    //最大行高
2 //setMaximumWidth(30);        //最大列宽
3 setMaximumSize(370, 150);    //设置最大尺寸
设置固定尺寸
设置Geometry

3、设置布局

1 QVBoxLayout *layout = new QVBoxLayout(this);
2 QLabel *label = new QLabel(QString("Holle,世界"), this);
3 layout->addWidget(label,Qt::AlignCenter);    //居中
4 //Qt::AlignCenter 中心对齐
5 //Qt::AlignLeft 左对齐
6 //Qt::AlignRight 右对齐
7 //QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列。 
8 //QVBoxLayout:垂直布局,在垂直方向上排列控件,即:上下排列。

4、添加常规可视图片

1 labelImg = new QLabel;
2 Image1.load(":/img/head.jpg");
3 labelImg->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
4 labelImg->setPixmap(QPixmap::fromImage(Image1));
//.h文件
5 //#include <QImage>
6 //private:
7 //QImage Image1;

5、添加圆形可视图片,即QQ头像类

两种方式,一种直接将图片修改为圆形的透明图片(*.png),一种则是使用蒙版形式,使用图片处理工具创建一个圆形透明图片(*.png),然后Qt操作代码如下:

//.cpp文件
    labelImg = new QLabel(this);
    //设置蒙版
    labelImg ->setMask(pixmapBack.mask());
    labelImg ->setStyleSheet("border-image:url(qrc:/img/mask_30x30.png)");//mask_30x30.png,是我做的圆形透明图片,已经导入到资源文件
    QPixmap head = QPixmap(":/img/head.jpg").scaled(QSize(labelImg->width(), labelImg->height()), Qt::KeepAspectRatio, Qt::SmoothTransformation);    //head.jpg,是要显示的图片类型大小随意,这里我用的是*.jpg。KeepAspectRatio:保持长宽比例
    labelImg ->setPixmap(head);

6、实现被点击事件

标签实现被点击事件有两种方式,一种是自定义一个按钮类标签,一种是采用事件过滤,具体的请看代码。注:这里采用了鼠标事件。

1).自定义按钮类标签:

自定义按钮类标签

2).事件过滤标签

事件过滤标签

7、实现超链接

1 label= new QLabel(tr("<style> a {text-decoration: none} </style><a href = http://haozip.2345.com>2345好压</a>"),this);
2 //文本无下划线:<style> a {text-decoration: none} </style>,如去掉则有下划线。

8、修改颜色

标签颜色有基本两处,一是文本颜色,二是背景颜色。而文本颜色设置两种,一种是用QPalette类,一种是StyleSheet类,代码如下:

1 label = new QLabel(this);
2 QPalette palette;
3 palette.setColor(QPalette::WindowText,Qt::red);
4 label->setPalette(palette);
5 //文本颜色:红色
 1 label = new QLabel(this);
 2 label -> setStyleSheet("QLabel { color: rgb(143,122,102);}");
 3 //label -> setStyleSheet("QLabel { color: red;}");
 4 //label -> setStyleSheet("QLabel { "color: #FF0000;";}");
 5 //常见颜色十六进制值
 6 //<font color=red或"#FF0000">红色</font>
 7 //<font color="#dd0000">浅红色</font>
 8 //<font color="#660000">深红色</font>
 9 //<font color="#00dd00">浅绿色</font>
10 //<font color="#006600">深绿色</font>
11 //<font color="#0000dd">浅蓝色</font>
12 //<font color="#000066">深蓝色</font>
13 //<font color="#dddd00">浅黄色</font>
14 //<font color="#666600">深黄色</font>
15 //<font color="#00dddd">浅青色</font>
16 //<font color="#006666">深青色</font>
17 //<font color="#dd00dd">浅紫色</font>
18 //<font color="#660066">深紫色</font>

 设置背景颜色:

1 label = new QLabel(this);
2 label ->setStyleSheet("background-color:lightred;");
3 //颜色值可通用

9、设置圆角标签,且扩展类。注:此圆角和第5点中的圆不同用。

1 label = new Qlabel(this);
2 label -> setStyleSheet("Qlabel{color: white;"
3                "border-radius: 20px;  border: 2px groove gray;border-style: outset;}"/*此处设置圆角*/
4                "Qlabel:hover{background-color:lightgreen; color: black;}"
5                "Qlabel:pressed{background-color:rgb(85, 170, 255);border-style: inset; }");
6     //注:当radius的值是控件高度或者宽度一半时可化作圆;border:边 hover:点燃 outset:常规 pressed:按下 inset:内嵌

好了,今天就先到这吧!如果你也有好的代码可以私信或者加以评论。如有疑问,可随时联系QQ:1285015525。

(本文只出现在Aili_Xiao的博客中,目前在博客园和CDSN中)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章