我正在手动创建一组 QLabels,这些 QLabels 被放入 QGridLayout 中,并且应该均匀分布。当我使用 QT 设计器创建一个测试表单并添加一系列标签并将它们放在 QGridLayout 中时,标签会占据网格中单元格的完整大小。当我在 C++ 中手动执行此操作时,标签不会扩展并显示文本的最小大小。我能够让这些标签垂直扩展,但不能水平扩展。
这是我创建 QGridLayout 的方式:
m_layout = new QGridLayout;
m_layout->setHorizontalSpacing( 1 );
m_layout->setVerticalSpacing( 1 );
m_layout->setContentsMargins(0,0,0,0);
m_layout->setMargin(0);
m_layout->setSizeConstraint( QGridLayout::SetDefaultConstraint );
//m_layout->setSizeConstraint( QGridLayout::SetMaximumSize );
当我更改大小约束时,它根本不会影响标签的大小,所以我认为问题可能在于我如何创建标签,情况如下:
QLabel *label = new QLabel;
label->setAlignment( Qt::AlignCenter );
label->setFrameStyle( QFrame::Raised );
label->setMinimumSize( QSize(0,0) );
label->setMaximumSize( QSize(16777215, 16777215) );
label->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
现在,所有 QLabel 的大小都相同,但最终它们的大小会有所不同,这就是我使用打包箱算法的原因。我围绕这样的类设计了算法:
struct ButtonFittingNode
{
ButtonFittingNode( QRect rectangle, QGridLayout *layout )
: used( false )
, node( rectangle )
, down( NULL )
, right( NULL )
, m_layout( layout )
{ }
~ButtonFittingNode()
{
if ( down ) delete down;
if ( right ) delete right;
}
ButtonFittingNode *findNode( QLabel *btn )
{
int w = 1;
int h = 1;
if ( this->used )
{
ButtonFittingNode *bfn = NULL;
if ( this->right ) bfn = this->right->findNode( btn );
if ( this->down && !bfn ) bfn = this->down->findNode( btn );
return bfn;
}
else if ( ( w <= node.width() ) && ( h <= node.height() ) )
{
qDebug() << "Placing at " << node.x() << node.y();
m_layout->addWidget( btn, node.y(), node.x(), w, h, Qt::AlignJustify );
this->used = true;
this->splitNode( QSize( w, h ) );
return this;
}
return NULL;
}
void splitNode( QSize sz )
{
int w = 0, h = 0;
// Create a down node
w = this->node.width();
h = this->node.height() - sz.height();
if ( h > 0 )
{
QRect n( this->node.x(), this->node.y() + sz.height(), w, h );
this->down = new ButtonFittingNode( n, m_layout );
}
// Create a right node
w = this->node.size().width() - sz.width();
h = sz.height();
if ( w > 0 )
{
QRect n( this->node.x() + sz.width(), this->node.y(), w, h );
this->right = new ButtonFittingNode( n, m_layout );
}
}
bool used;
QRect node;
ButtonFittingNode *down;
ButtonFittingNode *right;
private:
QGridLayout *m_layout;
};
使用 3x2 网格,我得到以下输出:
抱歉,我不得不模糊文本,因为这是一个与工作相关的项目,但灰色是背景,白色是标签的背景。如您所见,他们很高,但很瘦。我希望这些标签又高又胖。我是否设置了一些错误的属性?