[SOLVED] How to have QGridLayout with ALL columns having specific fixed width???



  • Basic Question
    How to make all columns in QGridLayout the exact same size?

    Basic Answer
    http://qt-project.org/forums/viewthread/47640/#195067

    My Custom Implementation of QGridLayout to Simplify/Streamline the Process
    http://qt-project.org/forums/viewthread/47640/#195124

    Original Question
    I have been trying everything, and I just cannot figure this out, I need ALL of the columns in my grid view to have a VERY specific fixed with so that I can properly use colspan to layout my items. Here's an example of what I'm looking for:

    (The COL headers don't exist in my layout, that is just for a visual reference on having fixed column width)

    | COL 0 | COL 1 | COL 2 | COL 3 | COL 4 | COL 5 |
    | ITEM 0 | | ITEM 1 | ITEM 2 | ITEM 3 | ITEM 4 |
    | ITEM 5 | | | ITEM 6 | ITEM 7 | ITEM 8 |

    And this is what I'm getting:
    Col 0 and 1 are taking half as much space as the rest

    | COL 0 & 1 | COL 2 | COL 3 | COL 4 | COL 5 |
    | ITEM 0 | ITEM 1 | ITEM 2 | ITEM 3 | ITEM 4 |
    | ITEM 5 | | ITEM 6 | ITEM 7 | ITEM 8 |

    All I can find online is using setColumnStretch(), but if I'm not mistaken this is the opposite of what I want, as it will cause columns to take up extra space, when I want them all to be the same.



  • Well I figured out a workaround, it's quite cumbersome, but I had to manually set fixed width for EVERY widget going into the table, and I also had to take into account the horizontal spacing of the grid view.

    Here's an example:
    This part is actually not entirely necessary
    @
    // Set minimum width of all columns
    for(int i = 0; i <= 5; i++) setColumnMinimumWidth(i, col_width);
    @

    This is the important part
    @
    /FOR EACH ITEM THAT ONLY TAKES 1 SPACE/
    widget1.setFixedWidth(col_width);
    widget2.setFixedWidth(col_width);
    // etc...

    /FOR ITEMS THAT TAKE MORE THAN 1 SPACE/
    // widget at ITEM 0 takes two spaces
    widget0.setFixedWidth(col_width * 2 + grid.horizontalSpacing());
    // widget at ITEM 5 takes 3 spaces
    widget0.setFixedWidth(col_width * 3 + grid.horizontalSpacing() * 2);
    @

    And I finally get this:
    | COL 0 | COL 1 | COL 2 | COL 3 | COL 4 | COL 5 |
    | ITEM 0 | | ITEM 1 | ITEM 2 | ITEM 3 | ITEM 4 |
    |ITEM 5 | | | ITEM 6 | ITEM 7 | ITEM 8 |

    But if anyone knows of a better way of doing this I'd still appreciate some tips/examples.



  • I had a whole bunch of items I was adding to the grid, so it was becoming quite cumbersome manually resizing every single widget, so I created a FixedGridLayout class to automate the process, this is what I've got:

    FixedGridLayout.hpp
    @
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    • FixedGridLayout.hpp *
    •                                                                         *
      
    • Copyright 2014 Tory Gaurnier tory.gaurnier@linuxmail.org *
    •                                                                         *
      
    • This program is free software; you can redistribute it and/or modify *
    • it under the terms of the GNU Lesser General Public License as published by *
    • the Free Software Foundation; version 3. *
    •                                                                         *
      
    • This program is distributed in the hope that it will be useful, *
    • but WITHOUT ANY WARRANTY; without even the implied warranty of *
    • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
    • GNU Lesser General Public License for more details. *
    •                                                                         *
      
    • You should have received a copy of the GNU Lesser General Public License *
    • along with this program. If not, see http://www.gnu.org/licenses/. *
                                                                                • */

    #ifndef FIXEDGRIDLAYOUT_HPP
    #define FIXEDGRIDLAYOUT_HPP

    #include <QGridLayout>

    class QWidget;

    /**

    • class FixedGridLayout
    • QGridView with fixed size cells, MUST be constructed with at least column width, row height and
    • parent QWidget* are optional. addItem() and addLayout() methods are made private so that only
    • widgets can be added, because only widgets can have fixed sizes, and that is required for this
    • class to work properly. If a layout needs to be added, just create a QWidget and use setLayout()
    • to give it your desired layout, then add the widget to FixedGridLayout.
    • IMPORTANT! When using this class it's VERY important that you don't
    • manually set the size of widgets being inserted, otherwise the outcome will not be as expected.
      */
      class FixedGridLayout : public QGridLayout {
      public:
      FixedGridLayout(int col_width, int row_height = -1);
      FixedGridLayout(QWidget *parent, int col_width, int row_height = -1);
      ~FixedGridLayout();
      void addWidget(QWidget *widget, int row, int col, Qt::Alignment alignment = 0);
      void addWidget(QWidget *widget, int row, int col, int row_span, int col_span,
      Qt::Alignment alignment = 0);

    private:
    int width, height;

    // Make addItem() and addLayout() private so only widgets can be added
    void addItem(QLayoutItem*, int, int, int, int, Qt::Alignment);
    void addLayout(QLayout*, int, int, Qt::Alignment);
    void addLayout(QLayout*, int, int, int rowSpan, int columnSpan, Qt::Alignment);
    };

    #endif
    @

    FixedGridLayout.cpp
    @
    #include "FixedGridLayout.hpp"

    #include <QDebug>
    #include <QWidget>

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    •    FIXEDGRIDLAYOUT PUBLIC METHODS           *
      
                                                                                                  • */

    FixedGridLayout::FixedGridLayout(int col_width, int row_height)
    : width(col_width), height(row_height) {}

    FixedGridLayout::FixedGridLayout(QWidget *parent, int col_width, int row_height)
    : QGridLayout(parent), width(col_width), height(row_height) {}

    FixedGridLayout::~FixedGridLayout() {}

    void FixedGridLayout::addWidget(QWidget *widget, int row, int col, Qt::Alignment alignment) {
    addWidget(widget, row, col, 1, 1, alignment);
    }

    void FixedGridLayout::addWidget(QWidget *widget, int row, int col, int row_span, int col_span,
    Qt::Alignment alignment) {
    // First set fixed width based on col_span and horizontal spacing
    widget->setFixedWidth((width * col_span) + (horizontalSpacing() * (col_span - 1)));

    // Next set fixed height based on row_span and vertical spacing, but only if height != -1
    if(height != -1) {
    widget->setFixedHeight((height * row_span) + (verticalSpacing() * (row_span - 1)));
    }

    // Last actually add widget to grid view
    QGridLayout::addWidget(widget, row, col, row_span, col_span, alignment);
    }
    @



  • And sorry for it being kinda squished together, I just copied and pasted, and I have my column limit in my advanced text editor set to 100.



  • If anyone is interested in using my code, FixedGridLayout is used exactly like QGridLayout, with a few exceptions, when it is initialized it must at least have a value for col_width, and the only add methods that are available are addWidget() methods, which are used exactly like from QGridLayout, the reason for this is the class depends on setting fixed width and height of the widgets, which is only possible for widgets, not layouts or layout items.

    I should also note that -I didn't actually test it with setting fixed row_height, it should work, but might need a little fine tuning-.
    Update
    OK, so I tested row_height and it works fine, also, if you need to add any kind of spacing, just add a blank QWidget, if you need vertical spacing then you must also set row_height when instantiating the grid layout.


Log in to reply
 

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