How to fill QGridLayout from top to left to right?

I would like to populate QGridLayout QWidgets . The QWidgets element should appear in the order from top to left to top right and begin to fill down down after each row is filled with QWidgets . An example of a similar and familiar GUI is how Apple sorts its applications on the iPhone or iPad home screen. Applications go from top to bottom right-up and continue to go down after filling each line.

Right now, whenever I add elements, they process the entire screen and / or are not added next to each other.

This is a sample code of what I'm doing.

 m_gridLayout = new QGridLayout(this); this->setLayout(m_gridLayout); m_gridLayout->addWidget(widgetToBeAdded, m_currentRow, m_currentColumn, Qt::AlignLeft); 

I will proceed to update m_currentColumn and m_currentRow, as expected. After a certain number of columns, I say to go to the next row, and nothing happens. I confirmed with debugging that it is infact, spitting out the correct rows and columns.)

In the end, I will need to press QScrollArea so that I can scroll down the grid.

The size of each QWidget will be the same. If I could get any help regarding proper grid sorting, this would be much appreciated.

EDIT: Using the answer below, I managed to get my items to sort in the correct order. However, there is a large space between all the elements in the vertical direction. This is not fixed by changing the verticalSpacing property, as I thought. Does anyone know how to proceed?

+6
source share
2 answers

Write your own grid layout, for example:

Headline

 #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 

A 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 the 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 element at a certain position, switch to the next position in the grid layout, which in my example has the maximum number of columns 4. I simply switch to the next column until I reach the fourth column. Then I reset the column to 0 and switch to the next row. I do this while there is an item. As soon as there is no element in this place, I placed my widget there.

This is just a quick example, but maybe you just need to work with it.

You have to improve it by handling errors, keep track of an infinite loop, etc.

+5
source

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

+3
source

Source: https://habr.com/ru/post/910464/


All Articles