QLabel text align wit StyleSheet
Is it possible to set the text-align for a QLabel with style sheet?
I tried the “obvious” QLabel { text-align: center; } but it doesn’t work.
14 replies
Did you try setAlignment [doc.qt.nokia.com]?
Sure, setAlignment works, but it has to be called from code. I’m interested in doing it with CSS style sheet.
Did you try setAlignment [doc.qt.nokia.com]?
In the reference of text-align wrote:
- This property is currently supported only by QPushButton and QProgressBar.
Only for alignment properties or for all supported property types [qt-project.org] ?
You will have to distinguish between
- Properties as in Style Sheet Properties [qt-project.org], like border, border-radius or background-color, which are part of the CSS specification
- Properties as in Qt’s Property System [qt-project.org], like QLabel::alignment, QLabel::text or QLabel::wordWrap, which are part of a QObject’s definition (using Q_PROPERTY)
You can use style sheets not only to set CSS properties, but also to set QObject properties, given that you prefix them with qproperty- (so for example to set the QLabel::alignment property you will have to use qproperty-alignment) so the engine knows you are referring to a QObject property, not a CSS property.
- QLabel label;
- label.setStyleSheet("border: 1px solid red;"
- "border-radius: 3px;"
- "background-color: white;"
- "qproperty-alignment: AlignCenter;"
- "qproperty-text: 'This is some rather long text.';"
- "qproperty-wordWrap: true;");
Lukas, you made my day! I just tested it and it works like a charm.
The only thing that i dont get working is to set something like this:
- QLabel label;
- label.setStyleSheet("qproperty-alignment: AlignBottom AlignRight;");
does this work ?
- m_label1 = new QLabel;
- m_label1->setText("<p style=\"color:red; text-align:justify;\">The <strong>Qt</strong>kdkdkd didds hd duddbs shdydyd djudhdyddd dhdydysisi jdjfjfjf pgphohkgjfufh dhdhdbhfjfn fnhfhfhfkjf fjfhfhfjhjfn jfjfjdigdfkfi ifgjufhkfjjfi gigigjgj gifiuffhfk kjfjfj jjfjfjgfj kigkgkgkgk kiggigim </p>");
- m_label1->setWordWrap(true);
The only thing that i dont get working …
You’ve forgotten the | (mind the quotes).
- QLabel label;
- label.setStyleSheet("qproperty-alignment: 'AlignBottom | AlignRight';");
You must log in to post a reply. Not a member yet? Register here!




