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. QLabels have same width regardless of text?
QtWS25 Last Chance

QLabels have same width regardless of text?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlabellayoutwidget
9 Posts 4 Posters 2.9k Views
  • 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.
  • H Offline
    H Offline
    hsetlik
    wrote on 9 Sept 2021, 15:02 last edited by
    #1

    So I have a QHBoxLayout that gets filled with instances of a subclass of QLabel. I want the spaces between one label's text and the next to all be the same, so I wrap the QHBoxLayout in a widget and try to limit that widget's width to a sum of each label's widths which was calculated when the labels were constructed prior to being added to any layout:

    //instantiate the labels
    int fullWidth = 0;
    for(auto& word : words)
    {
        auto label = new WordListLabel(word, this);
        fullWidth += label->width();
        wordListHBox->addWidget(label);
    }
    //wrap the layout in a widget
    auto wordListWidget = new QWidget(this);
    wordListWidget->setLayout(wordListHBox);
    wordListWidget->setMaximumWidth(fullWidth);
    

    Stepping through this in the debugger I found that every label, regardless of its text, is being instantiated with a width of 100. Is there some setting I have to adjust to get the labels to be sized based on the length of their text? Or is this just the wrong way to go about evenly spacing text in a QHBoxLayout? Any insight much appreciated.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Sept 2021, 15:07 last edited by
      #2

      You should try sizeHint() - since the widget is not shown yet, the width is a default value.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • H Offline
        H Offline
        hsetlik
        wrote on 9 Sept 2021, 15:14 last edited by
        #3

        You mean setting the label's sizeHint()?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 9 Sept 2021, 15:24 last edited by
          #4

          You can't set a sizeHint()... you should use it.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JoeCFD
            wrote on 9 Sept 2021, 16:02 last edited by JoeCFD 9 Sept 2021, 16:05
            #5

            use font to get bounding box of each word
            auto rect = QFontMetrics( font() ).boundingRect( word );
            its label->setFixedWidth( rect.width() );
            Then all labels with different width will have the same spacing.

            E 1 Reply Last reply 9 Sept 2021, 16:40
            0
            • J JoeCFD
              9 Sept 2021, 16:02

              use font to get bounding box of each word
              auto rect = QFontMetrics( font() ).boundingRect( word );
              its label->setFixedWidth( rect.width() );
              Then all labels with different width will have the same spacing.

              E Offline
              E Offline
              eyllanesc
              wrote on 9 Sept 2021, 16:40 last edited by eyllanesc 9 Sept 2021, 16:40
              #6

              @hsetlik try with: wordListWidget->setFixedWidth(wordListWidget->sizeHint().width());

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              1 Reply Last reply
              2
              • H Offline
                H Offline
                hsetlik
                wrote on 9 Sept 2021, 18:20 last edited by
                #7

                Sorry- where do you mean I should use it?

                E 1 Reply Last reply 9 Sept 2021, 18:44
                0
                • H hsetlik
                  9 Sept 2021, 18:20

                  Sorry- where do you mean I should use it?

                  E Offline
                  E Offline
                  eyllanesc
                  wrote on 9 Sept 2021, 18:44 last edited by
                  #8

                  @hsetlik replace wordListWidget->setMaximumWidth(fullWidth);

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  H 1 Reply Last reply 9 Sept 2021, 18:53
                  0
                  • E eyllanesc
                    9 Sept 2021, 18:44

                    @hsetlik replace wordListWidget->setMaximumWidth(fullWidth);

                    H Offline
                    H Offline
                    hsetlik
                    wrote on 9 Sept 2021, 18:53 last edited by
                    #9

                    @eyllanesc Oh I see, it's working now. Thank you!

                    1 Reply Last reply
                    0

                    8/9

                    9 Sept 2021, 18:44

                    • Login

                    • Login or register to search.
                    8 out of 9
                    • First post
                      8/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved