October 24, 2011

viktor.benei viktor.benei
Lab Rat
39 posts

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

October 24, 2011

Eddy Eddy
Area 51 Engineer
1557 posts

Did you try setAlignment [doc.qt.nokia.com]?

 Signature 

Moderator
Qt Certified Specialist
Nokia Qt Ambassador

October 24, 2011

viktor.benei viktor.benei
Lab Rat
39 posts

Sure, setAlignment works, but it has to be called from code. I’m interested in doing it with CSS style sheet.

Eddy wrote:
Did you try setAlignment [doc.qt.nokia.com]?

October 24, 2011

Ruzik Ruzik
Lab Rat
293 posts

In the reference of text-align wrote:

  1. This property is currently supported only by QPushButton and QProgressBar.

October 24, 2011

Ruzik Ruzik
Lab Rat
293 posts

I am suggest to use

  1. padding-left

But, i think, it is bad idea

October 25, 2011

viktor.benei viktor.benei
Lab Rat
39 posts

I didn’t spot the “only supported” part in the doc. So it’s not available yet… I guess I have to learn live without ;)

Thanks for the help.

January 28, 2012

kajmano kajmano
Lab Rat
4 posts

Viktor, have you tried this? It works for me…

QLabel { qproperty-alignment: AlignCenter; }

August 23, 2012

Stefan Walter Stefan Walter
Lab Rat
103 posts

It works for me too, but can someone explain why?

kajmano wrote:
Viktor, have you tried this? It works for me…

QLabel { qproperty-alignment: AlignCenter; }

August 23, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

You can set any designable property using the qproperty-* syntax, which includes the alignment property.

August 23, 2012

Stefan Walter Stefan Walter
Lab Rat
103 posts

Only for alignment properties or for all supported property types [qt-project.org] ?

August 23, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

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.

  1. QLabel label;
  2. label.setStyleSheet("border: 1px solid red;"
  3.                     "border-radius: 3px;"
  4.                     "background-color: white;"
  5.                     "qproperty-alignment: AlignCenter;"
  6.                     "qproperty-text: 'This is some rather long text.';"
  7.                     "qproperty-wordWrap: true;");

August 23, 2012

Stefan Walter Stefan Walter
Lab Rat
103 posts

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:

  1. QLabel label;
  2. label.setStyleSheet("qproperty-alignment: AlignBottom AlignRight;");

August 23, 2012

issam issam
Ant Farmer
144 posts

does this work ?

  1.  m_label1 = new QLabel;
  2.  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>");
  3.  m_label1->setWordWrap(true);
  4.  m_label1->setAlignment(Qt::AlignJustify);

 Signature 

http://www.iissam.com/

August 23, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

Stefan Walter wrote:
The only thing that i dont get working …

You’ve forgotten the | (mind the quotes).
  1. QLabel label;
  2. label.setStyleSheet("qproperty-alignment: 'AlignBottom | AlignRight';");

August 23, 2012

Stefan Walter Stefan Walter
Lab Rat
103 posts

Awesome, thanks a lot for all this tips.

 
  ‹‹ Convert QColor and QSize from and to QString      QSystemTrayIcon and shortcuts on OSX ››

You must log in to post a reply. Not a member yet? Register here!