Results 1 to 3 of 3

Thread: Should this work: Width of QLabel (with text)

  1. #1
    Join Date
    Nov 2016
    Location
    Florida, US
    Posts
    27
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Should this work: Width of QLabel (with text)

    I was experimenting with a way to do UI's that had some compartmentalized validation that was active, as opposed to just preventing a user from leaving the field as the current validator does.

    A side effect of this is I have a bunch of widgets, each composed of three pieces (QLabel, QLineEdit or other, QLabel), and I wanted to force that first QLabel in all of these to be the same width so the right side of them are aligned when the overall widget (the container of all three) is in a QVBoxLayout.

    My approach was to iterate through them (in the containing widget), find the needed width of each, and then set all of them (max and min) to the maximum width.

    It works perfectly in the sense of alignment, the widgets all function the way I want, but I am not getting anywhere near the correct size for the QLabel's width, so many of them are getting truncated.

    screen.jpg

    Notice how some lines are truncated on the left.

    Are there better ways to do this, with grid or such -- not sure, but can we leave that question for a moment.

    Is it practical to do what I want -- determine the size needed for the text?

    Here is how I do it:

    The prompt is declared:

    Qt Code:
    1. QLabel* p;
    To copy to clipboard, switch view to plain text mode 


    It is put inside a QHBoxLayout, with the other two widgets following (valueWidget is defined elsewhere)>

    Qt Code:
    1. hb = new QHBoxLayout(this);
    2. hb->setAlignment(Qt::AlignBottom);
    3.  
    4. m = new QLabel(this);
    5. m->setMinimumWidth(MUSICALPI_SETTINGS_MSG_MIN_WIDTH);
    6. p = new QLabel(prompt,this);
    7. p->setAlignment(Qt::AlignRight | Qt::AlignCenter);
    8. hb->addWidget(p);
    9. hb->addWidget(valueWidget);
    10. hb->addWidget(m);
    11. this->setObjectName(key);
    12. p->setStyleSheet("font-size:" + QString::number(MUSICALPI_SETTINGS_FORM_DATA_FONT_SIZE * 0.8) + "px;");
    13. valueWidget->setStyleSheet("font-size:" + QString::number(MUSICALPI_SETTINGS_FORM_DATA_FONT_SIZE) + "px; font-style: bold;");
    14. m->setStyleSheet("font-size:" + QString::number(MUSICALPI_SETTINGS_FORM_DATA_FONT_SIZE) + "px; color: darkRed;");
    15. hb->setContentsMargins(0,0,0,0);
    To copy to clipboard, switch view to plain text mode 

    And then to return its width a public function in this containing widget is called while iterating over all of them on the screen:

    Qt Code:
    1. int settingsItem::getPromptWidth()
    2. {
    3. int ret = p->fontMetrics().boundingRect(p->text()).width();
    4. qDebug() << "Width of '" << p->text() << "' is " << ret;
    5. return ret;
    6. }
    To copy to clipboard, switch view to plain text mode 

    It is returning items that track the actual prompt length (i.e. longer prompts result in bigger numbers), but the widths are much too small.

    When I use these to force the widget width, they get exactly the width I force (i.e. the problem is in the width calculation, not how I push it back to all widgets).

    And if I force the calculated width to be larger (e.g. double it) it all works fine, though that's basically hard coding (of sorts) which I was trying to avoid.

    Should the above work?

    is the container (which holds many of these individual triple-widgets) somehow affecting the calculation?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Should this work: Width of QLabel (with text)

    It isn't clear where you are calling this function to get the width, but remember that until the widget's showEvent() occurs, the sizes of the parent widget and children within it have not yet been finalized. If you are trying to do this in the constructor it generally won't work.

    So implement showEvent() for your parent widget and set the sizes there.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Linwood (4th March 2017)

  4. #3
    Join Date
    Nov 2016
    Location
    Florida, US
    Posts
    27
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Should this work: Width of QLabel (with text)

    Bingo.

    It was called from a containing widget but before anything did a "show" on the container, so these had not been shown either. I added a this->show() to the containing widget (that holds the layout for all these individual widgets I am sizing part of), just prior to looping through to get their lengths, and it worked almost perfectly. I think it's returning the width of the text, and there's probably still a frame or margin or something, so it was just a few (like 5) pixels too short. I added a 5% slop factor (which isn't too unreasonable I think) and it works fine; I tried a variety of phrases and changes in wording, and everything lines up perfectly.

    Thank you.

Similar Threads

  1. geting QLabel text ontop of other QLabel displaying image
    By krystosan in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2013, 17:35
  2. QLabel Set text not work after A few minutes
    By lamp in forum Qt Programming
    Replies: 2
    Last Post: 17th November 2012, 06:29
  3. don't work qlineargradient for text in QLabel
    By GreenScape in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2010, 16:59
  4. QLabel auto width?
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2009, 07:25
  5. Replies: 1
    Last Post: 18th February 2009, 08:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.