描述:
QLable文字对齐明明设置对齐方式是top对齐;显示确实buttom对方式。实践发现Qlabel对齐上下对齐方式是颠倒的!
QLabel不支持stylesheet设置对齐方式,qt stylesheet referenc中介绍中:
QLable框架“表现”上并不是maring,border,padding,content构成。设置border会影响文字的显示。但不知道具体具体怎么引起的?
例如:
border-top-width: 0px:
border-top-width: 4px时:
代码:
#include "widget.h"
#include <QApplication>
#include "QScrollArea"
#include "QVBoxLayout"
#include "QLabel"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget * parent = new QWidget();
parent->setWindowFlags(Qt::FramelessWindowHint);
parent->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
parent->setStyleSheet(QString("QWidget{background-color:transparent}\
QWidget:hover{background-color: #d9d9d9}"));
parent ->resize(100,60);
QLabel * icon = new QLabel(parent);
icon->setStyleSheet(QString("background-color: yellow") );
QLabel * text = new QLabel(parent);
text->setAlignment(Qt::AlignBottom|Qt::AlignHCenter);
text->setText("CNY");
text->setStyleSheet(QString("QWidget{background-color:red;font-size:18px;border-top-width: 0px;border-style: solid;border-color: darkblue;}"));
text->setFixedHeight(20);
QVBoxLayout *vlayout = new QVBoxLayout();
vlayout->addWidget(icon);
vlayout->addWidget(text);
vlayout->setSpacing(0);
vlayout->setContentsMargins(8,4,8,0);
parent->setLayout(vlayout);
parent->show();
return a.exec();
}