Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget subclass does not show up with invalid sizeHint()

QWidget subclass does not show up with invalid sizeHint()

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 979 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by
    #1

    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
    
    jsulmJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      QWidget always returns -1 as sizeHint.
      It simply has none.
      That is why.
      Add a sizeHint to ImageWidget and return something reasonable.

      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          What exactly are you trying to achieve with your widget ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          JoeCFDJ 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            What exactly are you trying to achieve with your widget ?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            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.

            mrjjM 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              JoeCFDJ 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                JoeCFDJ 1 Reply Last reply
                1
                • JoeCFDJ JoeCFD

                  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
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JoeCFDJ 1 Reply Last reply
                  2
                  • jsulmJ jsulm

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

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

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

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

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

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved