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. QDialog default layout is NULL
QtWS25 Last Chance

QDialog default layout is NULL

Scheduled Pinned Locked Moved Solved General and Desktop
qdialoglayoutfixedsizehintsetfixedsize
3 Posts 2 Posters 503 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.
  • K Offline
    K Offline
    kevin009
    wrote on last edited by kevin009
    #1

    Hello,

    I'm creating a dialog derived from QDialog. I'm designing it in the Qt Creator designer. I specified, in the designer, a vertical layout for my widgets. Everything works and looks fine. However, I'm trying to prevent the resizing of my dialog. After some research I saw that I could do this with QWidget::setFixedSize() and I could pass QWidget::sizeHint() as a parameter. The problem is that size hint returns a QSize of {-1, -1}. After looking at the source code it appears sizeHint returns this if no layout is assigned to the QDialog i.e. d->layout == nullptr. I thought that assigning a layout in the designer would set the d->layout member in QWidget. Apparently I was wrong.
    My question is, how can I get a valid sizeHint without programatically setting the layout of my QDialog?

    Thank you,
    Kevin

    C 1 Reply Last reply
    0
    • K kevin009

      Hello,

      I'm creating a dialog derived from QDialog. I'm designing it in the Qt Creator designer. I specified, in the designer, a vertical layout for my widgets. Everything works and looks fine. However, I'm trying to prevent the resizing of my dialog. After some research I saw that I could do this with QWidget::setFixedSize() and I could pass QWidget::sizeHint() as a parameter. The problem is that size hint returns a QSize of {-1, -1}. After looking at the source code it appears sizeHint returns this if no layout is assigned to the QDialog i.e. d->layout == nullptr. I thought that assigning a layout in the designer would set the d->layout member in QWidget. Apparently I was wrong.
      My question is, how can I get a valid sizeHint without programatically setting the layout of my QDialog?

      Thank you,
      Kevin

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @kevin009 If the QDialog has a vertical layout applied to it then QWidget::layout() will return a non-null value. If the layout contains child widgets then you will get a valid QWidget::sizeHint(). A default Dialog project started with Creator, with a vertical layout and text edit placed in the dialog:

      #include "widget.h"
      #include <QDebug>
      #include <QApplication>
      
      int main(int argc, char *argv[]) {
          QApplication a(argc, argv);
          Widget w;
          qDebug() << "Layout" << w.layout() << w.sizeHint();
          w.show();
          return a.exec();
      }
      
      Layout 0x55baa5b16560 QSize(278, 214)
      

      Note that applying a vertical layout to the dialog:

      b2bbbbab-85ed-48c9-8512-8869ef5542f1-image.png

      is not the same thing as dropping vertical layout in the dialog's client area.

      efe6eb85-83c6-4cd3-908c-29e4e5518f33-image.png

      Doing this, leads to the result you describe.

      K 1 Reply Last reply
      2
      • C ChrisW67

        @kevin009 If the QDialog has a vertical layout applied to it then QWidget::layout() will return a non-null value. If the layout contains child widgets then you will get a valid QWidget::sizeHint(). A default Dialog project started with Creator, with a vertical layout and text edit placed in the dialog:

        #include "widget.h"
        #include <QDebug>
        #include <QApplication>
        
        int main(int argc, char *argv[]) {
            QApplication a(argc, argv);
            Widget w;
            qDebug() << "Layout" << w.layout() << w.sizeHint();
            w.show();
            return a.exec();
        }
        
        Layout 0x55baa5b16560 QSize(278, 214)
        

        Note that applying a vertical layout to the dialog:

        b2bbbbab-85ed-48c9-8512-8869ef5542f1-image.png

        is not the same thing as dropping vertical layout in the dialog's client area.

        efe6eb85-83c6-4cd3-908c-29e4e5518f33-image.png

        Doing this, leads to the result you describe.

        K Offline
        K Offline
        kevin009
        wrote on last edited by
        #3

        @ChrisW67 Thanks for replying. I was doing as you suggested but still getting the behaviour I specified. Turns out I had a conflicting ui generated header file that had the manually added layout which explains why I was experiencing a NULL layout. Once I deleted the conflicting file everything worked as expected.
        Thanks for the extremely easy to follow instructions.

        Kevin

        1 Reply Last reply
        0
        • SGaistS SGaist has marked this topic as solved on

        • Login

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