where is my QLabel getting resized?
-
Hi all -
In a .ui file, I've added a QLabel, and promoted it to my own LogoLabel so I can set an image on it:
I've tried multiple sizePolicy settings, but my label always seems to be resized to 100w by 30h (verified with qDebug() output in c'tor).
LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { // setScaledContents(true); qDebug() << width() << height();Any idea where that could be creeping in? Thanks...
-
Hi,
The constructor is the wrong place to ask for the size. The first time the size is valid is when showEvent is called.
Until a widget is shown, it has no "physical presence" and thus the size you get can by anything.
-
Hi,
The constructor is the wrong place to ask for the size. The first time the size is valid is when showEvent is called.
Until a widget is shown, it has no "physical presence" and thus the size you get can by anything.
@SGaist thank you. This solves part of the problem. I need to scale the image to fit my QLabel. If I try to do it in the c'tor, I run into the problem you pointed out:
LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { m_pixmap = new QPixmap(filename); *m_pixmap = m_pixmap->scaled(width(), height(), Qt::KeepAspectRatio); // bzzt }My only other method for this class is the paintEvent() override. It seems like a waste of CPU to scale in this method. Do I need to create another method for the scaling, and call it from somewhere in my application?
Thanks...
-
Why do you need to reimplement the paintEvent method ?
By the way, there's no need to allocate QPixmap on the heap.
-
Why do you need to reimplement the paintEvent method ?
By the way, there's no need to allocate QPixmap on the heap.
-
@SGaist this is how someone (in this forum) advised me to do it, probably because I was trying to have the logo automatically resize with the main widget.
If there's a simpler way to do this, I'm 100% open to it...
@mzimmers said in where is my QLabel getting resized?:
because I was trying to have the logo automatically resize with the main widget.
-
@mzimmers said in where is my QLabel getting resized?:
because I was trying to have the logo automatically resize with the main widget.
@Christian-Ehrlicher I'm doing that, though that call too is currently in my c'tor() -- is that a problem?
@SGaist I use a pointer to QPixmap so I can give it the filename at the same time -- saves a line of code. Maybe not such a good idea, but I don't think it's affecting this issue.
-
@Christian-Ehrlicher I'm doing that, though that call too is currently in my c'tor() -- is that a problem?
@SGaist I use a pointer to QPixmap so I can give it the filename at the same time -- saves a line of code. Maybe not such a good idea, but I don't think it's affecting this issue.
@mzimmers said in where is my QLabel getting resized?:
though that call too is currently in my c'tor()
It's not. And you don't need a custom class for this.
so I can give it the filename at the same time
?
m_pixmap(filename) in the member init list maybe? Apart from this - where does
filenamecomes from? -
@mzimmers said in where is my QLabel getting resized?:
though that call too is currently in my c'tor()
It's not. And you don't need a custom class for this.
so I can give it the filename at the same time
?
m_pixmap(filename) in the member init list maybe? Apart from this - where does
filenamecomes from?const QString filename(":/logos/Fluidra_Blue.png"); class LogoLabel : public QLabel { Q_OBJECT private: QPixmap *m_pixmap; protected: void paintEvent(QPaintEvent *event) override; ... LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { m_pixmap = new QPixmap(filename); setScaledContents(true); }And, my image isn't scaling -- it's just truncating.
EDIT: I removed the use of my LogoLabel class and just specified the file (and scaling) in design mode, and it seems to work. I can't remember why I'd originally created a subclass to begin with (this was on another project from a couple years ago), but...don't argue with success, I guess.
Thanks for the help.
-
@mzimmers said in where is my QLabel getting resized?:
And, my image isn't scaling -- it's just truncating.
setScaledContents() works as advertised:
#include <QApplication> #include <QLabel> class LogoLabel : public QLabel { Q_OBJECT public: explicit LogoLabel(QWidget *parent = nullptr); }; LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { setPixmap(QPixmap("test.png")); setScaledContents(true); } int main(int argc, char **argv) { QApplication app(argc, argv); LogoLabel l; l.resize(640, 480); l.show(); return app.exec(); } #include "main.moc"It does not maintain the aspect ratio of the source image, which you are clearly trying to do.
This version does:
include <QApplication> #include <QLabel> #include <QResizeEvent> class LogoLabel : public QLabel { Q_OBJECT public: explicit LogoLabel(QWidget *parent = nullptr); protected: void resizeEvent(QResizeEvent *event); }; LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { setPixmap(QPixmap("test.png")); //setScaledContents(true); } void LogoLabel::resizeEvent(QResizeEvent *event) { QPixmap tempPixmap = pixmap(Qt::ReturnByValue); setPixmap(tempPixmap.scaled(event->size(), Qt::KeepAspectRatio)); } int main(int argc, char **argv) { QApplication app(argc, argv); LogoLabel l; l.resize(640, 480); l.show(); return app.exec(); } #include "main.moc" -
@mzimmers said in where is my QLabel getting resized?:
And, my image isn't scaling -- it's just truncating.
setScaledContents() works as advertised:
#include <QApplication> #include <QLabel> class LogoLabel : public QLabel { Q_OBJECT public: explicit LogoLabel(QWidget *parent = nullptr); }; LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { setPixmap(QPixmap("test.png")); setScaledContents(true); } int main(int argc, char **argv) { QApplication app(argc, argv); LogoLabel l; l.resize(640, 480); l.show(); return app.exec(); } #include "main.moc"It does not maintain the aspect ratio of the source image, which you are clearly trying to do.
This version does:
include <QApplication> #include <QLabel> #include <QResizeEvent> class LogoLabel : public QLabel { Q_OBJECT public: explicit LogoLabel(QWidget *parent = nullptr); protected: void resizeEvent(QResizeEvent *event); }; LogoLabel::LogoLabel(QWidget *parent) : QLabel(parent) { setPixmap(QPixmap("test.png")); //setScaledContents(true); } void LogoLabel::resizeEvent(QResizeEvent *event) { QPixmap tempPixmap = pixmap(Qt::ReturnByValue); setPixmap(tempPixmap.scaled(event->size(), Qt::KeepAspectRatio)); } int main(int argc, char **argv) { QApplication app(argc, argv); LogoLabel l; l.resize(640, 480); l.show(); return app.exec(); } #include "main.moc"@ChrisW67 said in where is my QLabel getting resized?:
resizeEvent
Thanks, Chris - I'm having some measure of success just skipping my LogoLabel class altogether. I did stumble across another oddity regarding scaling, though -- in Design mode, when I specify a large image (and select ScaledContents), it fits...until I try to put that image in a layout, at which point it reverts to original size. Is this expected behavior?
Thanks...
EDIT: some pictures might help explain what I'm seeing. Prior to layout:
Getting ready to layout:
After the horizontal layout:
-
The layout will take on a size dictated by the sizeHints() of the contained objects and any space allocated to it by its parent layout.
Given the way the layout resized over/under others it looks to me like your outer, containing widget does not have layout applied, e.g. a QVBoxLayout. -
The layout will take on a size dictated by the sizeHints() of the contained objects and any space allocated to it by its parent layout.
Given the way the layout resized over/under others it looks to me like your outer, containing widget does not have layout applied, e.g. a QVBoxLayout. -
@ChrisW67 that does help. Now, though, my QPlainTextEdit boxes don't resize to fill the main window. I've set the horizontal size policy to Expanding, and also tried MinimumExpanding, but the width remains fixed. I thought this would cause the boxes to expand horizontally -- what might I be doing wrong?
Thanks...EDIT: I found my problem - cockpit error. I was selecting everything inside the main widget, instead of selecting the main widget itself. Oops...
-
setScaledContents(true); does not keep the aspect ratio of the image. If you want to keep the ratio, do the following:
- override resizeEvent to get new label size to compute icon size with the same aspect ratio
- override paintEvent(QPaintEvent *event) to paint the icon with the icon size from step 1.
