Qt:如何禁用“缩小以适合"?为QGridLayout? [英] Qt: How to disable the "shrink-to-fit" for a QGridLayout?

查看:84
本文介绍了Qt:如何禁用“缩小以适合"?为QGridLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows7上使用Qt5.
在我当前的应用程序中,我尝试显示和删除许多按钮.

I'm using Qt5 on Windows7.
In my current app I try to display and remove a number of push-buttons.

widget = new ButtonWidget(ui->frame); // frame is a QScrollArea
connect(ui->addBtns,    SIGNAL(clicked()), widget, SLOT(addButtons()));
connect(ui->deleteBtns, SIGNAL(clicked()), widget, SLOT(deleteButtons()));

ButtonWidget 类在这里:

ButtonWidget::ButtonWidget(QWidget * parent) : QWidget(parent)
{
   gridLayout = new QGridLayout(parent);
}

static QStringList texts{...}; // various content (see explanations below)

void ButtonWidget::addButtons()
{
    for(auto i = 0; i < texts.size(); i++)
    {
        gridLayout->addWidget(new QPushButton(texts[i], this), i / 5, i % 5);
    }
}

void ButtonWidget::deleteButtons()
{
    while(gridLayout->count())
    {
        delete gridLayout->itemAt(0)->widget();
    }
}

如果我设置了 QStringList文本{"1 \ nok"}; ,我会得到:

...这很丑陋(在所有布局上居中并水平扩展).

If I set QStringList texts{"1\nok"}; I get this:

...which is ugly (centered and expanded horizontally on all layout).

如果我设置了 QStringList文本{"1 \ nok",...,"24 \ nok"}; ,我会得到:

...似乎还可以.

If I set QStringList texts{"1\nok",...,"24\nok"}; I get this:

...which is/seems quite ok.

最后,如果我设置 QStringList文本{"1 \ nok",...,"36 \ nok"}; ,我会得到:

...非常糟糕,乱码等

And finally, if I set QStringList texts{"1\nok",...,"36\nok"}; I get this:

...which is very bad, garbled, etc.

因此,问题是:有什么方法可以解决此问题,即以某种方式禁用版式上的缩小以适合"吗?默认按钮大小就可以了.
我想利用QScrollArea的垂直滚动功能,而不是在(有限的)可用空间中塞满并塞满所有按钮...

So, the question: Is there any way to fix this, i.e. to disable somehow this "shrink-to-fit" on the layout? A default button size would be just fine.
I would like to take advantage of the vertical-scroll feature of the QScrollArea, not to stuff and cram all the buttons in the (limited) available space...

推荐答案

如果仅将子窗口小部件添加到 QScrollArea ,您将不会获得任何滚动,因为这些窗口小部件不受区域以任何方式.您需要使用 QScrollArea :: setWidget 来设置窗口小部件:

If you merely add child widgets to a QScrollArea, you won't get any scrolling, since these widgets are not managed by the area in any way. You need to set the widget using QScrollArea::setWidget instead:

widget = new ButtonWidget;
ui->frame->setWidget(widget);

这篇关于Qt:如何禁用“缩小以适合"?为QGridLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆