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 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
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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 last edited by
        #3

        You mean setting the label's sizeHint()?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #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.

            eyllanescE 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              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.

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by eyllanesc
              #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 last edited by
                #7

                Sorry- where do you mean I should use it?

                eyllanescE 1 Reply Last reply
                0
                • H hsetlik

                  Sorry- where do you mean I should use it?

                  eyllanescE Offline
                  eyllanescE Offline
                  eyllanesc
                  wrote on 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
                  0
                  • eyllanescE eyllanesc

                    @hsetlik replace wordListWidget->setMaximumWidth(fullWidth);

                    H Offline
                    H Offline
                    hsetlik
                    wrote on last edited by
                    #9

                    @eyllanesc Oh I see, it's working now. 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