How does one fill a QGridLayout from top-left to right?

后端 未结 2 810
深忆病人
深忆病人 2021-01-05 09:35

I would like to fill a QGridLayout with QWidgets. The QWidgets need to appear in a top-left to top-right fashion and proceed to fill

相关标签:
2条回答
  • 2021-01-05 09:55

    The FlowLayout from this Qt example might be what you are looking for.

    0 讨论(0)
  • 2021-01-05 10:05

    Write your own grid layout, something like this:

    Header

    #ifndef MY_GRID_LAYOUT_H
    #define MY_GRID_LAYOUT_H
    
    #include <QGridLayout>
    
    class my_grid_layout : public QGridLayout
    {
    public:
        my_grid_layout(QWidget *parent, int max_column_count );
        ~my_grid_layout();
    
        void add_widget( QWidget* p_widget );
    
    private:
        int m_max_column_count;
    };
    
    #endif // MY_GRID_LAYOUT_H
    

    Source

    #include "my_grid_layout.h"
    
    my_grid_layout::my_grid_layout(QWidget *parent, int max_column_count)
        : QGridLayout(parent)
    {
        m_max_column_count = max_column_count;
    }
    
    my_grid_layout::~my_grid_layout()
    {
    
    }
    
    void my_grid_layout::add_widget( QWidget* p_widget )
    {
        int current_row = 0;
        int current_column = 0;
    
        while( itemAtPosition(current_row, current_column) != 0 )
        {
            if( current_column == (m_max_column_count-1) )
            {
                current_column = 0;
                ++current_row;
            }
            else
            {
                ++current_column;
            }
        }
    
        QGridLayout::addWidget( p_widget, current_row, current_column );
    }
    

    Test in a main window

    #include "test_1.h"
    
    Test_1::Test_1(QWidget *parent, Qt::WFlags flags)
        : QMainWindow(parent, flags)
    {
        m_grid_layout = new my_grid_layout(this,4);
    
        m_grid_layout->add_widget( new QPushButton( "Widget 0", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 1", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 2", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 3", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 4", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 5", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 6", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 7", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 8", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 9", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 10", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 11", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 12", this ) );
        m_grid_layout->add_widget( new QPushButton( "Widget 13", this ) );
    
    
    
        QWidget* central_widget = new QWidget(this);
        central_widget->setLayout(m_grid_layout);
    
        setCentralWidget(central_widget);
    }
    
    Test_1::~Test_1()
    {
    
    }
    

    Result
    enter image description here

    While there is already an item at a certain position, switch to the next position in the grid layout, which has a maximum column count of 4 in my example. I just switch to next column until I reached the 4th column. Then I reset the column to 0 and switch to the next row. I do this as long as there is already an item. As soon as there is no item in that place, I put my widget there.

    This is just a quick example, but maybe it's enough for you to work with.

    You should enhance it by error handling, watch out for endless loop and such...

    0 讨论(0)
提交回复
热议问题