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中)

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