QDialog default layout is NULL
-
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 passQWidget::sizeHint()
as a parameter. The problem is that size hint returns aQSize
of{-1, -1}
. After looking at the source code it appearssizeHint
returns this if no layout is assigned to theQDialog
i.e.d->layout == nullptr
. I thought that assigning a layout in the designer would set thed->layout
member in QWidget. Apparently I was wrong.
My question is, how can I get a validsizeHint
without programatically setting the layout of my QDialog?Thank you,
Kevin -
@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:
is not the same thing as dropping vertical layout in the dialog's client area.
Doing this, leads to the result you describe.
-
@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
-