Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Aligning qlabel text in centre
Forum Updated to NodeBB v4.3 + New Features

Aligning qlabel text in centre

Scheduled Pinned Locked Moved Solved General and Desktop
qlabel
8 Posts 5 Posters 107.3k Views 3 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.
  • S Offline
    S Offline
    Sumit
    wrote on last edited by
    #1

    I am trying to create a label on which I can display my image and above it a qlabel text for saying this is my input image and then setting both of them in vertical layout. But text always gets allign to the left. I also tried allign the text at the centre and then apply vertical layout but doesn't work. I am using Qt designer.
    I also used horizontal spacer with qlabel text and set them to horizontal layout, then adding horizontal layout and image display label in vertical layout but nothing seems to work.

    Is there any better way to do that.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Are you setting the alignment on the label or on the layout? It should be on the label.
      Here's an example:

      label alignment

      S 2 Replies Last reply
      3
      • Vinod KuntojiV Offline
        Vinod KuntojiV Offline
        Vinod Kuntoji
        wrote on last edited by Chris Kawa
        #3

        @Sumit ,

        Widget::Widget(QWidget *parent)
            : QWidget(parent)
        {
            m_ImageLabel = new QLabel;
            m_TextLabel = new QLabel;
            m_VLayout = new QVBoxLayout;
        
            QPixmap pixmap("Image.png");
            m_ImageLabel->setPixmap(pixmap);
            m_TextLabel->setText("Text");
        
            m_VLayout->addWidget(m_TextLabel,Qt::AlignCenter);
            m_VLayout->addWidget(m_ImageLabel,Qt::AlignCenter);
        
            this->setLayout(m_VLayout);
        }
        

        C++, Qt, Qt Quick Developer,
        PthinkS, Bangalore

        Chris KawaC D 2 Replies Last reply
        1
        • Vinod KuntojiV Vinod Kuntoji

          @Sumit ,

          Widget::Widget(QWidget *parent)
              : QWidget(parent)
          {
              m_ImageLabel = new QLabel;
              m_TextLabel = new QLabel;
              m_VLayout = new QVBoxLayout;
          
              QPixmap pixmap("Image.png");
              m_ImageLabel->setPixmap(pixmap);
              m_TextLabel->setText("Text");
          
              m_VLayout->addWidget(m_TextLabel,Qt::AlignCenter);
              m_VLayout->addWidget(m_ImageLabel,Qt::AlignCenter);
          
              this->setLayout(m_VLayout);
          }
          
          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          @Vinod-Kuntoji That's not gonna work. This code aligns the label itself to the horizontal center of the layout, but since neither of the labels have maximum size constraints they will both occupy the same amount of space, filling the layout, so this alignment doesn't matter.
          What you need to do is align the text inside the label, not the label itself i.e. m_TextLabel->setAlignment(Qt::AlignCenter).

          1 Reply Last reply
          4
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by Pradeep Kumar
            #5

            Hi,

            @Sumit ,

            u can use as mentioned by @Chris-Kawa , to align text in middle of QLabel,

            m_label->setStyleSheet("background-color:red;");
            m_label->setText("Qt QML");
            m_label->setAlignment(Qt::AlignCenter);
            

            This may help u ,

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            1 Reply Last reply
            5
            • Chris KawaC Chris Kawa

              Are you setting the alignment on the label or on the layout? It should be on the label.
              Here's an example:

              label alignment

              S Offline
              S Offline
              Sumit
              wrote on last edited by
              #6

              @Chris-Kawa yes i do it on label only, but when I put them in vertical layout it moves to left.

              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                Are you setting the alignment on the label or on the layout? It should be on the label.
                Here's an example:

                label alignment

                S Offline
                S Offline
                Sumit
                wrote on last edited by
                #7

                @Chris-Kawa I got the mistake, I was keeping size policy fixed due to which allignment has no effect on the text label.

                Thank you everyone. It got solved

                1 Reply Last reply
                3
                • Vinod KuntojiV Vinod Kuntoji

                  @Sumit ,

                  Widget::Widget(QWidget *parent)
                      : QWidget(parent)
                  {
                      m_ImageLabel = new QLabel;
                      m_TextLabel = new QLabel;
                      m_VLayout = new QVBoxLayout;
                  
                      QPixmap pixmap("Image.png");
                      m_ImageLabel->setPixmap(pixmap);
                      m_TextLabel->setText("Text");
                  
                      m_VLayout->addWidget(m_TextLabel,Qt::AlignCenter);
                      m_VLayout->addWidget(m_ImageLabel,Qt::AlignCenter);
                  
                      this->setLayout(m_VLayout);
                  }
                  
                  D Offline
                  D Offline
                  danBesu
                  wrote on last edited by
                  #8

                  @Vinod-Kuntoji nice example, thank you!

                  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 Extensions
                  • Unsolved