How do I explicitely position QLabels?
-
I am trying to create a vertical scale with correctly positioned numeric labels. Something like this (sorry about the background colour):
My code looks like this:
// Temperature Scale Box
Tscale = new QGroupBox(this);
Tscale->setGeometry(QRect(1200, 150, 90, 550));
Tscale->setFixedWidth(90);
Tscale->setMaximumSize(QSize(90, 2000));QWidget *TSLayoutWidget = new QWidget(Tscale); TSLayoutWidget->setGeometry(QRect(0, 0, 90, 550)); QHBoxLayout *TSLayout = new QHBoxLayout(TSLayoutWidget); // Display Temperature Colour Bar QLabel *Tbar = new QLabel(TSLayoutWidget); Tbar->setObjectName(QStringLiteral("Tbar")); Tbar->setGeometry(0, 0, 30, 512); Tbar->setPixmap(QPixmap::fromImage(QColourBar)); TSLayout->addWidget(Tbar); // Display Temperature Scale Legend QVBoxLayout *verticalLayout = new QVBoxLayout(); QList<QLabel *> markers; for (int i = 0; i < 9; ++i){ markers << new QLabel(TSLayoutWidget); markers.at(i)->setAlignment(Qt::AlignLeft | Qt::AlignBottom); markers.at(i)->setText(QString::number((8-i)*100+600)+"\u2103"); markers.at(i)->setGeometry(0, 512-i*512/8, 45, 15); verticalLayout->addWidget(markers.at(i)); qDebug() << "marker " << i << "text: " << markers.at(i)->text() << ", geometry: " << markers.at(i)->geometry(); } TSLayout->addLayout(verticalLayout);
The debug output looks like this:
Debug: 2019-04-01 14:40:46.619: marker 0 text: "1400℃" , geometry: QRect(0,512 45x15)
Debug: 2019-04-01 14:40:46.619: marker 1 text: "1300℃" , geometry: QRect(0,448 45x15)
Debug: 2019-04-01 14:40:46.619: marker 2 text: "1200℃" , geometry: QRect(0,384 45x15)
Debug: 2019-04-01 14:40:46.619: marker 3 text: "1100℃" , geometry: QRect(0,320 45x15)
Debug: 2019-04-01 14:40:46.619: marker 4 text: "1000℃" , geometry: QRect(0,256 45x15)
Debug: 2019-04-01 14:40:46.619: marker 5 text: "900℃" , geometry: QRect(0,192 45x15)
Debug: 2019-04-01 14:40:46.620: marker 6 text: "800℃" , geometry: QRect(0,128 45x15)
Debug: 2019-04-01 14:40:46.620: marker 7 text: "700℃" , geometry: QRect(0,64 45x15)
Debug: 2019-04-01 14:40:46.620: marker 8 text: "600℃" , geometry: QRect(0,0 45x15)The geometry values are being correctly set by the setGeometry() but it has no influence on the placement of the labels. Placement seems to be purely determined by the order that they are added and dividing the available space between the total number of labels.
In other contexts setGeometry works as I expect and places widgets relative to the parent widget. I don't understand what is different in this case, or what I'm doing wrong?
Any suggestions will be much appreciated.
Mark
-
Hi,
You are adding your labels to a layout, therefore the layout may change their size to accommodate the space it has. If you want your labels precisely at one place, you should manually place them without forgetting to update on resize.
Note that you could also consider re-implementing the paint event to draw the text exactly where you want them rather than use additional labels.
-
@SGaist Thank you very much for the quick reply.
I've removed both the horizontal and vertical layout boxes and everything works exactly as I want.
Sometimes newbies make life more complicated for themselves than it needs to be!
Thanks again!