Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel text align wit StyleSheet
Qt 6.12 LTS is out! See what's new in the release blog

QLabel text align wit StyleSheet

Scheduled Pinned Locked Moved General and Desktop
15 Posts 7 Posters 97.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Did you try "setAlignment":http://doc.qt.nokia.com/4.7/qlabel.html#alignment-prop?

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        [quote author="Eddy" date="1319480478"]Did you try "setAlignment":http://doc.qt.nokia.com/4.7/qlabel.html#alignment-prop?[/quote]

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          In the reference of text-align wrote:
          @This property is currently supported only by QPushButton and QProgressBar.@

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I am suggest to use
            @padding-left@
            But, i think, it is bad idea

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kajmano
                wrote on last edited by
                #7

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

                QLabel {
                qproperty-alignment: AlignCenter;
                }

                1 Reply Last reply
                7
                • W Offline
                  W Offline
                  walteste
                  wrote on last edited by
                  #8

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

                  [quote author="kajmano" date="1327775417"]Viktor, have you tried this? It works for me...

                  QLabel {
                  qproperty-alignment: AlignCenter;
                  }
                  [/quote]

                  1 Reply Last reply
                  1
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      walteste
                      wrote on last edited by
                      #10

                      Only for alignment properties or for all "supported property types":http://qt-project.org/doc/qt-4.8/stylesheet-reference.html#list-of-properties ?

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        You will have to distinguish between

                        • Properties as in "Style Sheet Properties":http://qt-project.org/doc/qt-4.8/stylesheet-reference.html#list-of-properties, like border, border-radius or background-color, which are part of the CSS specification
                        • Properties as in "Qt's Property System":http://qt-project.org/doc/qt-4.8/properties.html, 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;");
                        @

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          walteste
                          wrote on last edited by
                          #12

                          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;");
                          @

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            issam
                            wrote on last edited by
                            #13

                            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);
                            m_label1->setAlignment(Qt::AlignJustify);
                            @

                            http://www.iissam.com/

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              [quote author="Stefan Walter" date="1345718064"]The only thing that i dont get working ...[/quote]
                              You've forgotten the | (mind the quotes).
                              @
                              QLabel label;
                              label.setStyleSheet("qproperty-alignment: 'AlignBottom | AlignRight';");
                              @

                              1 Reply Last reply
                              1
                              • W Offline
                                W Offline
                                walteste
                                wrote on last edited by
                                #15

                                Awesome, thanks a lot for all this tips.

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt
                                • Unsolved