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?