QWidget subclass does not show up with invalid sizeHint()
-
I created a subclass of QWidget to display a picture.
#include <QWidget>
#include <QPixmap>class ImageWidget : public QWidget
{
Q_OBJECT
public:
explicit ImageWidget(QWidget* parent = Q_NULLPTR);const QPixmap & pixmap() const; void setPixmap( const QPixmap & pix_map );
protected:
void paintEvent(QPaintEvent*);private:
QPixmap m_pixmap;
QPixmap m_cachedPixmap;
};This class works fine in a small dialog. But when I put it in my app,
QSize size0 = parent->sizeHint();
auto image_widget = new ImageWidget( parent );
//image_widget->setMinimumSize( QSize( 4, 4 ) );
//auto image_widget = new QLabel( parent );
QSize size1 = image_widget->sizeHint();
size1 is always (-1, -1) although size0 is valid. The same result even when I set minimum size for image_widget.
If I use a QLabel as image_widget, size1 is valid; What is the problem? Thanks for your help.Joe
-
Hi
QWidget always returns -1 as sizeHint.
It simply has none.
That is why.
Add a sizeHint to ImageWidget and return something reasonable. -
Thanks for your reply. I can set QSize(4, 4) to sizeHint, for example. But this widget does not resize with parent layout if this is done.
Why does sizeHint work in my small test dialog program, but not within my app? My test program does not have any specific extra settings for this widget. -
Hi,
What exactly are you trying to achieve with your widget ?
-
Basically I wanted to show an image with a subclass of QWidget. The image will resize properly when the GUI app resizes. In this case paintEvent will be called and the original image will be rescaled. The widget works fine in a small test code with a single QDialog + vertical layout. The image resizes when the dialog resizes. No problem at all. But the sizeHint is always invalid when this widget is used in the GUI app. paintEvent is never called.
-
Basically I wanted to show an image with a subclass of QWidget. The image will resize properly when the GUI app resizes. In this case paintEvent will be called and the original image will be rescaled. The widget works fine in a small test code with a single QDialog + vertical layout. The image resizes when the dialog resizes. No problem at all. But the sizeHint is always invalid when this widget is used in the GUI app. paintEvent is never called.
@JoeCFD
And in the other app, the widget is also in a layout ?
SizeHint is like a recommended size so unless widget is set to zero size, it should
not affect paintEvent. (or its hidden)
For test, try to call resize() on the widget in the real app. just as test. -
But aren't you re-impementing QLabel and its scaledContents property ?
-
I created a subclass of QWidget to display a picture.
#include <QWidget>
#include <QPixmap>class ImageWidget : public QWidget
{
Q_OBJECT
public:
explicit ImageWidget(QWidget* parent = Q_NULLPTR);const QPixmap & pixmap() const; void setPixmap( const QPixmap & pix_map );
protected:
void paintEvent(QPaintEvent*);private:
QPixmap m_pixmap;
QPixmap m_cachedPixmap;
};This class works fine in a small dialog. But when I put it in my app,
QSize size0 = parent->sizeHint();
auto image_widget = new ImageWidget( parent );
//image_widget->setMinimumSize( QSize( 4, 4 ) );
//auto image_widget = new QLabel( parent );
QSize size1 = image_widget->sizeHint();
size1 is always (-1, -1) although size0 is valid. The same result even when I set minimum size for image_widget.
If I use a QLabel as image_widget, size1 is valid; What is the problem? Thanks for your help.Joe
@JoeCFD See https://doc.qt.io/qt-5/qwidget.html#sizeHint-prop
"The default implementation of sizeHint() returns an invalid size if there is no layout for this widget".
You did not overwrite sizeHint() and you did not put this widget in a layout, so I would say this is expected. -
@JoeCFD See https://doc.qt.io/qt-5/qwidget.html#sizeHint-prop
"The default implementation of sizeHint() returns an invalid size if there is no layout for this widget".
You did not overwrite sizeHint() and you did not put this widget in a layout, so I would say this is expected. -
But aren't you re-impementing QLabel and its scaledContents property ?
-
@JoeCFD
And in the other app, the widget is also in a layout ?
SizeHint is like a recommended size so unless widget is set to zero size, it should
not affect paintEvent. (or its hidden)
For test, try to call resize() on the widget in the real app. just as test.@mrjj If sizeHint is invalid, it means the size of the widget does not change at all after layout. And paintEvent will not be called, right? Is it not kind of odd to call resize() explicitly? Qlabel does not need it. In my test program, it is not needed as well.