Qt学习笔记外观篇(六):QLabel

  QLabel非常简单,且非常实用。

 其典型用法如下:

	QLabel label(QObject::tr("hello world"),&dialog);
	label.setFrameStyle(QFrame::Panel | QFrame::Sunken);
	label.setAlignment(Qt::AlignBottom | Qt::AlignRight);
产生QLabel对象,设置外观,设置对齐方式。

通过以下代码,产生QLabel:

int main(int argc, char *argv[])
{

	QApplication app(argc, argv);
	QFile file(":/qss/style.qss");
	file.open(QFile::ReadOnly);
	QDialog dialog;
	QLabel label(QObject::tr("hello world"),&dialog);
	label.setFrameStyle(QFrame::Panel | QFrame::Sunken);
	label.setAlignment(Qt::AlignBottom | Qt::AlignRight);
	QLabel label2(QObject::tr("hello China"),&dialog);
	QVBoxLayout layout(&dialog);
	layout.addWidget(&label);
	layout.addWidget(&label2);
	dialog.setLayout(&layout);
	//dialog.setStyleSheet(file.readAll());
	dialog.show();

	return app.exec();
}

产生QLabel如下:


我们通过qss进行自定义外观,我们需要自定义的选项有:background(背景颜色,背景图片。。),border,padding,margin,文字的大小,颜色,字体等等。

QLabel {
    font: 9pt;
	font-family:"Vrinda";
    color: rgb(0, 0, 127);
	border: 2px solid green;
    border-radius: 4px;
     padding: 2px;
     background-image: url(images/background.png);
}
QLabel:hover{
    font: 9pt;
	font-family:"Vrinda";
    color: rgb(0, 0, 127);
	border: 2px solid green;
    border-radius: 4px;
    padding: 2px;
	 background-image:url();
}

我们得到:

Qt学习笔记外观篇(六):QLabel_第1张图片

关于使用QStyle类进行定制QLabel的方法我还不会,如果大家知道方法的话,请留言。


你可能感兴趣的:(Qt)