Important: Please read the Qt Code of Conduct -

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
    explicit ImageWidget(QWidget* parent = Q_NULLPTR);

    const QPixmap & pixmap() const;
    void setPixmap( const QPixmap & pix_map ); 

    void paintEvent(QPaintEvent*);

    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.


  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion


    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    But aren't you re-impementing QLabel and its scaledContents property ?

  • Qt Champions 2019

    @JoeCFD See
    "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.

  • @jsulm After it is put in the layout, the sizeHint() is still invalid.

  • @SGaist This one works. But I am still puzzled by the fact that the widget does not work in the same way as in the small test code.

  • @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.

Log in to reply