天天看点

label qt 自动换行_Qt之QLabel

这边整理一下由这篇文章中汇总的几个用法,因为代码还算是比较简单的,所以我就不重复了。

对齐方式

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;

label qt 自动换行_Qt之QLabel

snipaste_20170407_160812.png