Non-resizeable QDialog with fixed size in Qt?

QtQdialog

Qt Problem Overview


I have a Qt dialog application. Now I dont want that dialog to be resizeable. I am not sure how to achieve this. I tried a bunch of things but still when the dialog launches this dialog can be resized.

What is the property that i should set to disable the dialog/Widget resize.

I also tried

setSizePolicy(QSizePolicy::Fixed);

But i get an error saying..

source\nimcac_settingsMain.cpp(36) : error C2248:
'QSizePolicy::QSizePolicy' : cannot access private member declared in class 'QSizePolicy'
p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui../../src\gui
kernel\qsizepolicy.h(177) : see declaration of 'QSizePolicy::QSizePolicy' p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui../../src\gui
kernel\qsizepolicy.h(34) : see declaration of 'QSizePolicy'

Kindly help me out with this.

Qt Solutions


Solution 1 - Qt

The compile error you get is because you try to pass a QSizePolicy::Policy to setSizePolicy(QSizePolicy), but there's no implicit conversion from QSizePolicy::Policy (which is the policy for one dimension) to QSizePolicy (which is a class containing, among other things, one Policy per dimension (height, width)). QSizePolicy doesn't work on top-level widgets (windows) anyway, though.

setFixedSize() only works if you know the size of the dialog in advance (and usually you don't, what with changing font sizes and languages). You can do

window()->setFixedSize( window()->sizeHint() );

but it's much better to use

window->layout()->setSizeConstraint( QLayout::SetFixedSize );

That lets the layout determine the size of the dialog, but doesn't allow resizing, which I assume is what you were asking for.

Solution 2 - Qt

I don't know if you already tried it, but QWidget::setFixedSize should do what you want

Solution 3 - Qt

this->setFixedSize(this->width(),this->height());

Solution 4 - Qt

You need to change the windowFlags of the dialog and set it to Qt::MSWindowsFixedSizeDialogHint.

This only works in windows.

For more information please see this example: http://doc.qt.digia.com/4.5/widgets-windowflags.html

Solution 5 - Qt

On QT Creator, in the UI editor, click on the top object in the properties window, then scroll at the bottom in the Layout part. You should see the layoutSizeConstraint property.

Set the layoutSizeConstraint to SetFixedSize.

Solution 6 - Qt

If you use QtCreator (of course you are) you can set the property HorizontalsizePolicy to fixed and Vertical Policy also to Fixed. Then you can set the maximumSize to the dimensions you want. The window will not maximise again.

Solution 7 - Qt

In code you can do something like this,

Dialog->resize(581, 292);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(WaterLevelEditorDialog->sizePolicy().hasHeightForWidth());
Dialog->setSizePolicy(sizePolicy);
Dialog->setMinimumSize(QSize(581, 292));
Dialog->setMaximumSize(QSize(581, 292));
Dialog->setSizeGripEnabled(false);

In QtCreator do as follows,

  1. Select the dialog box widget
  2. Locate the dialog box widget in Object Window
  3. In Object Window, right click on dialog box object to popup a menu
  4. Select "Size Constraints" -> "Set Maximum Size" from the menu
  5. Right click again on dialog object to popup a menu
  6. Select "Size Constraints" -> "Set Minimum Size"
  7. In Property Window,
    • ensure "sizePolicy"->"Horizontal Policy" has value "Fixed"
    • ensure "sizePolicy"->"Vertical Policy" has value "Fixed"
    • ensure "sizeGripEnabled" is not checked

Solution 8 - Qt

From the Qt documentation, setSizePolicy() method either takes zero argument or two arguments but cannot be one argument. That's why you get this compilation error. From my experiment, if you don't set the fixed size. This method has no use. The window can still be resizable.

Solution 9 - Qt

An easier way is to set the maxium size to 0.

this->setMaxiumSize(QSize(0, 0));

Solution 10 - Qt

In case you are designing UI in QML and launching using QDeclarativeView, try the below code.

QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
.
.
.
//To make the window non-resizable
viewer->setFixedSize(viewer->width(),viewer->height());

Here QmlApplicationViewer is derived from QDeclarativeView.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAMMView Question on Stackoverflow
Solution 1 - QtMarc Mutz - mmutzView Answer on Stackoverflow
Solution 2 - QtCaoticView Answer on Stackoverflow
Solution 3 - QtSamirView Answer on Stackoverflow
Solution 4 - QtJesus FernandezView Answer on Stackoverflow
Solution 5 - QtjlguenegoView Answer on Stackoverflow
Solution 6 - QtetsoftView Answer on Stackoverflow
Solution 7 - QtshrikantdView Answer on Stackoverflow
Solution 8 - QtPixelsTechView Answer on Stackoverflow
Solution 9 - QtPatitotectiveView Answer on Stackoverflow
Solution 10 - QtAkaanthan CcoderView Answer on Stackoverflow