這邊整理一下由這篇文章中彙總的幾個用法,因為代碼還算是比較簡單的,是以我就不重複了。
對齊方式
setAlignment(Qt::AlignCenter);
setStyleSheet("qproperty-alignment: 'AlignBottom | AlignRight';");
自動換行
setWordWrap(true);
設定行高
QString strHeightText = "
%2
";
這個是通過html語言來實作行高的
省略
QString strElidedText = pLabel->fontMetrics().elidedText(strText, Qt::ElideRight, 200, Qt::TextShowMnemonic);
垂直顯示
pLabel->setText(strText.split("", QString::SkipEmptyParts).join("\n"));
pLabel->setAlignment(Qt::AlignCenter);
一般是水準顯示,這個主要是在每個字後面添加換行符
圖像
QPixmap pixmap(":/Images/logo");
pLabel->setPixmap(pixmap);
pLabel->setFixedSize(100, 100);
pLabel->setScaledContents(true);
動畫
QMovie *pMovie = new QMovie(":/Images/movie");
pLabel->setMovie(pMovie);
pLabel->setFixedSize(135, 200);
pLabel->setScaledContents(true);
pMovie->start();
一些有關QLabel的簡單用法
顯示一段時間後消失的label框
主要适用于提示資訊,有些類似于web的消息提示。
static void ShowMsg(QLabel *msg,QString str="",int timer = 2000)
{
msg->setText(str);
msg->show();
QEventLoop loop;
QTimer countTimer;
connect(&countTimer,SIGNAL(timeout()),&loop,SLOT(quit()));
countTimer.start(timer);
loop.exec();
msg->hide();
}
後續可以添加成為從右端劃入顯示,過一段時間自動消失。
Qt有很多的效果都可以參照web裡面的效果來進行實作,比如下面的提示框的Qss
color: rgb(101, 113, 128);
padding:5px;
border:1px solid #ddd;
border-left: 4px solid #39f;
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIkNTM2M2Y3QmYwYGNvwFcvwVbvNmL1h2cuFWaq5yd3d3Lc9CX6MHc0RHaiojIsJye.jpg)
snipaste_20170407_160812.png