這邊整理一下由這篇文章中匯總的幾個用法伐割,因為代碼還算是比較簡單的粤剧,所以我就不重復了捏卓。
對齊方式
setAlignment(Qt::AlignCenter);
setStyleSheet("qproperty-alignment: 'AlignBottom | AlignRight';");-
自動換行
setWordWrap(true);
-
設置行高
QString strHeightText = "<p style=\"line-height:%1%\">%2<p>";
這個是通過html語言來實現(xiàn)行高的
-
省略
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();
}
后續(xù)可以添加成為從右端劃入顯示硼婿,過一段時間自動消失轧葛。
Qt有很多的效果都可以參照web里面的效果來進行實現(xiàn)杆查,比如下面的提示框的Qss
color: rgb(101, 113, 128);
padding:5px;
border:1px solid #ddd;
border-left: 4px solid #39f;
snipaste_20170407_160812.png