Anyone can help me on making my mainwindow resizable?



  • So this is my mainwindow.ui.. well if u design the app from the designer you won't be able to give the user the option to resize the window which is kinda bad..

    !http://i.imgur.com/5uayX.png(1)!

    So i will try to make it via code.. The upper part of the ui is a widget ( with background ) containing 5 buttons and 4 seperators ( 1 pixel width ).. next is a stacked widget changing pages from the buttons in the upper part, and down there is a button , a layout and a combobox..

    This is my code:

    @void MainWindow::createOptionsGroupBox()
    {
    optionsGroupBox = new QGroupBox();
    optionsGroupBoxLayout = new QGridLayout;
    optionsGroupBoxLayout->addWidget(ui->widget, 0, 0, 1, 9);
    optionsGroupBoxLayout->addWidget(ui->musicButton, 0,0);
    optionsGroupBoxLayout->addWidget(ui->sep1, 0, 1);
    optionsGroupBoxLayout->addWidget(ui->videoButton,0,2);
    optionsGroupBoxLayout->addWidget(ui->sep2,0,3);
    optionsGroupBoxLayout->addWidget(ui->imageButton, 0,4);
    optionsGroupBoxLayout->addWidget(ui->sep3,0,5);
    optionsGroupBoxLayout->addWidget(ui->rom_deviceButton, 0,6);
    optionsGroupBoxLayout->addWidget(ui->sep4,0,7);
    optionsGroupBoxLayout->addWidget(ui->advancedButton, 0,8);
    optionsGroupBoxLayout->addWidget(ui->stackedWidget, 1, 0, 1, 9);
    optionsGroupBox->setLayout(optionsGroupBoxLayout);
    }

    void MainWindow::createButtonsLayout()
    {
    buttonsLayout = new QHBoxLayout;
    buttonsLayout->addWidget(ui->output_folder);
    buttonsLayout->addStretch();
    buttonsLayout->addWidget(ui->label_2);
    buttonsLayout->addWidget(ui->after_convertion2);
    buttonsLayout->addWidget(ui->after_convertion_icon);
    }@

    My first question is, how to have the widget thing sticked at the top the right and the left of the window, meaning without creating an empty space...

    !http://i.imgur.com/JRUft.png(2)!



  • Is MainWindow class derived from QMainWindow? Or QWidget? Anyway make sure that you set the parent for your mainLayout (Note that QMainWindow has a "centralWidget":http://doc.qt.nokia.com/4.7-snapshot/qmainwindow.html#centralWidget). I say this because optionsGroupBox doesn't get a parent at least from what i see.
    And why do you manually add to layout (in code) widgets that are created with the designer?



  • You have to have your mainWindow inherit a layout as well...right click on your centralWidget if your using a mainWindow and inherit the type of layout you want, or if your using a widget, just right click on the window pane and add the layout (its the tab the says "layout").

    Make sure to set your size constraints on all of your objects though so you can resize them how you want them.

    Also, you can put layouts inside of layouts..inside of layouts...so for example:
    7 buttons on the right put in a vertical layout
    put the main list view inside of a horizontal layout, and also put the layout containing the 7 buttons inside of that same horizontal layout...you get my drift? Also you can set the button widths to fixed horizontally, and the list view to expand vertically and horizontally.

    Fun fun.

    I'm assuming that array of buttons on the top is in a QFrame, inherit a horizontal layout in the QFrame (anything that is a QWidget can use layouts), then set all of the buttons size policies to expanding horizontally, and fixed vertically. Then set the frame size policy to fixed vertically (squeeze up to the buttons), and expanding horizontally. And you can adjust the outer margins / spacing's of the layout however you want them (this can all be done through the designer).



  • This is what i have in my constructor.. Have i done anything wrong?

    @ createOptionsGroupBox();
    createButtonsLayout();
    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(optionsGroupBox);
    mainLayout->addLayout(buttonsLayout);
    QWidget *widget = new QWidget();

    widget->setLayout(mainLayout);
    setCentralWidget(widget);@


  • you can also use

    @mainLayout->setContentsMargins(QMargins(0,0,0,0));@

    to remove the space.



  • Your code would be 10x simpler / life would be easy if you used the creator and the interface builder to make your layouts. That way everything is already visually right in front of you, your not creating any layouts from code, and you can test your resizing without having to build your application...Just saying! :)

    That the one benefit I find with the interface builder and Qt compared to .net / vb6 / c#, its nice to be able to see all of your stylesheets / layouts before compilation.



  • [quote author="Sam" date="1347552031"]you can also use

    @mainLayout->setContentsMargins(QMargins(0,0,0,0));@

    to remove the space.[/quote]

    You mean:
    @createOptionsGroupBox();
    createButtonsLayout();
    mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(QMargins(0,0,0,0));
    mainLayout->addWidget(optionsGroupBox);
    mainLayout->addLayout(buttonsLayout);
    QWidget *widget = new QWidget();

    widget->setLayout(mainLayout);
    setCentralWidget(widget);@
    

    Nothing changed...



  • [quote author="dvez43" date="1347552995"]Your code would be 10x simpler / life would be easy if you used the creator and the interface builder to make your layouts. That way everything is already visually right in front of you, your not creating any layouts from code, and you can test your resizing without having to build your application...Just saying! :)

    That the one benefit I find with the interface builder and Qt compared to .net / vb6 / c#, its nice to be able to see all of your stylesheets / layouts before compilation.[/quote]

    what exactly do u mean? if i make it by the designer debug it and resize it is obvious this will happen:

    !http://i.imgur.com/cxg5w.png(1)!



  • Well just use layouts the way you use them in your code already, only interactive via QtDesigner.
    see http://doc.qt.nokia.com/4.7-snapshot/designer-layouts.html



  • [quote author="DerManu" date="1347562268"]Well just use layouts the way you use them in your code already, only interactive via QtDesigner.
    see http://doc.qt.nokia.com/4.7-snapshot/designer-layouts.html[/quote]

    No it's really a lot more confusing than code.. So anyone can help do this via code?
    My problems:

    1. I don't want that empty space i want from it to be sticked

    !http://i.imgur.com/JRUft.png(1)!

    And no @ mainLayout->setContentsMargins(QMargins(0,0,0,0));@

    doesn't work

    1. as i have said the upper buttons change the page of the stackedwidget.. Well know that i have done it this way everytime it passes from the
      @ui->stackedWidget->setCurrentIndex(page_number_here);@

    it simply crashes.. i have no idea why this is happening



  • Since you are also using optionsGroupBoxLayout and buttonsLayout so try setting the contentsmargins for both as well

    @optionsGroupBoxLayout->setContentsMargins(QMargins(0,0,0,0));
    buttonsLayout->setContentsMargins(QMargins(0,0,0,0));@

    This should work .



  • [quote author="Sam" date="1347609987"]Since you are also using optionsGroupBoxLayout and buttonsLayout so try setting the contentsmargins for both as well

    @optionsGroupBoxLayout->setContentsMargins(QMargins(0,0,0,0));
    buttonsLayout->setContentsMargins(QMargins(0,0,0,0));@

    This should work .[/quote]

    No it doesn't work! See it yourself...



  • From the documentation Of QGridLayout

    bq. QGridLayout also includes two margin widths: the contents margin and the spacing(). The contents margin is the width of the reserved space along each of the QGridLayout's four sides. The spacing() is the width of the automatically allocated spacing between neighboring boxes.

    bq. The default contents margin values are provided by the style. The default value Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout.

    and QBoxLayout

    bq. QBoxLayout also includes two margin widths:

    • setContentsMargins() sets the width of the outer border on each side of the widget. This is the width of the reserved space along each of the QBoxLayout's four sides.
    • setSpacing() sets the width between neighboring boxes. (You can use addSpacing() to get more space at a particular spot.

    bq. The margin default is provided by the style. The default margin most Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout.

    So If you are using Qt Designer you can directly select the layout/centralWidget and then set the margins as 0,0,0,0
    like :-
    !http://imageshack.us/a/img585/5520/captureaq.png(margins)!



  • I did it by the designer and my only problems are: http://qt-project.org/forums/viewthread/20353/#97928


Log in to reply
 

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