简体   繁体   English

在QGridLayout中拉伸QLabel

[英]Stretching a QLabel in a QGridLayout

I am manually creating a set of QLabels that are being put into a QGridLayout, and should be distributed evenly. 我正在手动创建一组放置在QGridLayout中的QLabel,并且应该均匀分布。 When I create a test form using the QT Designer and add a series of labels, and put them in a QGridLayout, the labels occupy the full size of their cells in the grid. 当我使用QT设计器创建测试表单并添加一系列标签并将其放置在QGridLayout中时,标签将占据其网格中单元格的完整大小。 When I do this manually in c++, the labels don't expand and say the minimum size for the text. 当我在c ++中手动执行此操作时,标签不会展开并说出文本的最小大小。 I am able to get these labels to expand vertically, but not horizontally. 我能够使这些标签垂直扩展,但不能水平扩展。

Here is how I'm creating the QGridLayout: 这是我创建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 );

When I change the size constraint, it does not affect the size of the labels at all, so then I assumed that the issue may lie on how I'm creating the labels, which happens like this: 当我更改大小约束时,它根本不会影响标签的大小,因此,我认为问题可能在于我如何创建标签,这种情况是这样发生的:

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 );

Right now, all the QLabels are the same size, but eventually they will have differing sizes, which is why I'm using a packed-bin algorithm. 现在,所有QLabel的大小都相同,但是最终它们将具有不同的大小,这就是为什么我使用打包bin算法的原因。 I designed the algorithm around classes like this: 我围绕这样的类设计了算法:

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;
};

Using a 3x2 grid, I get the following output: 使用3x2网格,我得到以下输出:

在此处输入图片说明

Sorry, I had to blur the text, since this is a work related project, but grey is background, white is the label's background. 抱歉,我必须模糊文本,因为这是与工作相关的项目,但是灰色是背景,白色是标签的背景。 As you can see, they are tall, but skinny. 如您所见,它们很高,但很瘦。 I want these labels to be tall and fat. 我希望这些标签又高又胖。 Am I setting some of the attributes wrong? 我是否将某些属性设置错误?

This is all really simple. 真的很简单。 The size constraint of the layout has got nothing to do with what you're seeing. 布局的大小限制与您所看到的无关 You're also including a bunch of useless boilerplate code. 您还包括了许多无用的样板代码。 Get rid of explicit setting of minimum and maximum sizes unless you have some non-default sizes in mind. 除非明确考虑了一些非默认尺寸,否则请摆脱对最小和最大尺寸的明确设置。 This is completely useless: 这是完全没有用的:

label->setMinimumSize( QSize(0,0) );
label->setMaximumSize( QSize(16777215, 16777215) );

You need to set the label's sizePolicy to Expanding in the horizontal direction. 您需要将标签的sizePolicy设置为在水平方向上Expanding That's all there's to it: 这就是全部:

label->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );

I see what you want from your bin packing system, but it won't work very well as-is, since the column/row sizes in the grid will vary as the grid gets resized. 我看到了您希望从装箱系统中得到的东西,但是它并不能很好地工作,因为网格的列/行大小会随着网格大小的变化而变化。 Your packer will choose some column/row spans based on then current row/column sizes. 您的包装员将根据当前行/列的大小选择某些列/行跨度。 Upon resizing, the packer's decisions won't be adequate anymore. 调整大小后,打包机的决策不再足够。

What you really want is a custom layout that does all the packing on the fly. 您真正想要的是一种自定义布局,可以即时进行所有打包。 The flow layout example may be very close to what you want. 流布局示例可能与您想要的非常接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM