简体   繁体   中英

Qt setGeometry to negative value doesn't work

Im working with QT and I have a form with a QLabel in a QFrame. I want to set the QLabel's geometry so the bottom part of the QLabel is in the same place of the bottom of the frame. Since the label is longer than the frame, it's y coordinate should be negative.

int pos =  ui->imageFrame->height() - ui->imageLabel->pixmap()->height();
ui->imageLabel->setGeometry(0, pos, ui->imageFrame->width(), p.height());

Although when printing the QLabel's geometry, the y coordinate is correct, the label is showing on the upper part of the frame.

Help is much appreciated.

You can set the label's alignment with setAlignment . Here's a working example:

#include <QtWidgets>

#include "MyWidget.h"

MyWidget::MyWidget()
{
  setFixedSize(200,200);
  QLabel *label = new QLabel;
  label->setPixmap(QPixmap("/some/image/file.jpg"));
  label->setAlignment(Qt::AlignBottom);
  QHBoxLayout *hbox = new QHBoxLayout;
  hbox->addWidget(label);
  hbox->setContentsMargins(0,0,0,0);
  setLayout(hbox);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM