一个如何从左上到右填充QGridLayout? [英] How does one fill a QGridLayout from top-left to right?
问题描述
我想用QWidgets
填充QGridLayout
. QWidgets
需要以从左上到右上的方式显示,并在每行用QWidgets
填充后继续向下向下填充.一个类似且熟悉的GUI的示例是Apple如何在iPhone或iPad的主屏幕上对其应用程序进行排序.这些应用程序从左上到右上,并在每行填满后继续向下.
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 the down downwards after each row is filled with QWidgets
. An example of a similar and familiar GUI is how Apple sorts its apps on the iPhone or iPad's home screen. The apps go top-left to top-right and proceed to go downward after each row is filled.
现在,每当我添加元素时,它们就会占据整个屏幕,并且/或者不会彼此相邻添加.
Right now, whenever I add elements, they take up the entirety of the screen and/or aren't added next to each other.
这是我在做什么的示例代码
This is example code of what I am doing
m_gridLayout = new QGridLayout(this);
this->setLayout(m_gridLayout);
m_gridLayout->addWidget(widgetToBeAdded, m_currentRow, m_currentColumn, Qt::AlignLeft);
我继续按预期更新m_currentColumn和m_currentRow.在一定数量的列之后,我告诉它更改为下一行,什么也没有发生.我已经通过调试确认,它实际上是在吐出正确的行和列.)
I proceed to update m_currentColumn and m_currentRow as expected. After a certain amount of columns, I tell it to change to the next row, and nothing happens. I have confirmed through debugging that it is infact spitting out the correct rows and columns.)
最终,我将需要戴上QScrollArea
,以便人们可以向下滚动网格.
Eventually, I will need to throw on a QScrollArea
so that one can scroll down the grid.
每个QWidget
的大小将相同.如果我在正确排序网格方面能获得任何帮助,将不胜感激.
The size of each QWidget
will be the same. If I could get any help in regards to sorting the grid properly, it would be much appreciated.
使用下面的答案,我设法使我的物品按正确的顺序排序.但是,在垂直方向上的所有元件之间存在大量空间.正如我想的那样,这不能通过更改verticalSpacing属性来消除.有人知道如何进行吗?
Using the answer below, I have managed to get my items to sort in the correct order. However, there is a large amount of space in between all elements in the vertical direction. This is not obviated by changing the verticalSpacing property, as I would think. Does anyone know how to proceed?
推荐答案
编写自己的网格布局,如下所示:
Write your own grid layout, something like this:
标题
#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
来源
#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 );
}
在主窗口中测试
#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()
{
}
结果
虽然在某个位置已经有一个项目,但是切换到网格布局中的下一个位置,在我的示例中,该位置的最大列数为4.我只是切换到下一列,直到到达第四列.然后,我将该列重置为0,然后切换到下一行.只要已经有一个项目,我就这样做.一旦那个地方没有物品,我就把小部件放在那里.
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...
这篇关于一个如何从左上到右填充QGridLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!