Set fixed size of QMainWindow created in Designer



  • Good day, colleagues!

    How can I fix size of QMainWindow created in Designer? Fix size means that I want to see main window as I created it in Designer without ability to resize.

    I've tried this:

    @ui->setupUi(this);
    centralWidget()->layout()->setSizeConstraint(QLayout::SetFixedSize);@

    But got segfault.



  • To fix the size of the main window itself just call:

    @setFixedSize( myDesiredSize );@

    It's a member of QWidget which of course QMainWIndow inherits.



  • try this:
    this->setFixedSize(500,500);

    edit:
    didnt noticed that you allready gave the answer ZapB. :)



  • Thank you very much, this works, but is there any way to automatically get myDesiredSize from Designer?

    With current solution I should manually copy width and height values from Designer MainWindow geometry.



  • Did you try to set both maximum and minimum size in Designer? Right click on your main form and select Size Constraints -> Set Minimum Size and then Size Constraints -> Set Maximum Size.



  • try this:
    @ this->setFixedSize(this->geometry().width(),this->geometry().height());@

    edit:
    "this" is not needed.



  • Thank you, both solutions work, but to my mind, second is more elegant.



  • @marko-a,
    Its possible to set your maxsize the same as your minsize, but why do that if you have a nice function (setFixedSize) for it to do the job?



  • You can also get the preferred size from the sizeHint() functionality and use that as the argument in the setFixedSize() call. That way if your central widget ever changes your app will automatically calculate the new preferred size - as long as the central widget implements a sensible sizeHint of course.



  • [quote author="vinb" date="1303204074"]@marko-a,
    Its possible to set your maxsize the same as your minsize, but why do that if you have a nice function (setFixedSize) for it to do the job?[/quote]

    True, but the original question was:

    [quote author="usamytch" date="1303198694"]
    How can I fix size of QMainWindow created in Designer? Fix size means that I want to see main window as I created it in Designer without ability to resize.
    [/quote]

    Thus if you wish to solve problem by using only Designer you can do so by setting minimum & maximum size constraints.

    Cheers!



  • They are both valid solutions. In fact they amount to the same thing since that is all that the setFixedSize() function does anyway.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.