为什么QScrollArea的大小受到限制? [英] Why is the QScrollArea restricted in size?

查看:590
本文介绍了为什么QScrollArea的大小受到限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从 QWidget 继承的自定义窗口小部件中,我添加了 QScrollArea ,如下所示:

To my custom widget, inherited from QWidget, I have added a QScrollArea like this:

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)//MainWindow is a QWidget
{
    auto *scrollArea = new QScrollArea(this);
    auto *widget = new QWidget(this);

    widget->setStyleSheet("background-color:green");

    scrollArea->setWidget(widget);
    scrollArea->setWidgetResizable(true);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    QVBoxLayout *parentLayout = new QVBoxLayout(widget);

    this->setStyleSheet("background-color:blue");

    for(int i=0;i<12;i++){
        QHBoxLayout* labelLineEdit = f1();
        parentLayout->addStretch(1);
        parentLayout->addLayout(labelLineEdit);
    }

    parentLayout->setContentsMargins(0,0,40,0);
}

QHBoxLayout* MainWindow::f1()
{

    QHBoxLayout *layout = new QHBoxLayout;

    QLabel *label = new QLabel("Movie");
    label->setStyleSheet("background-color:blue;color:white");
    label->setMinimumWidth(300);
    label->setMaximumWidth(300);

    layout->addWidget(label);

    QLineEdit *echoLineEdit = new QLineEdit;
    echoLineEdit->setMaximumWidth(120);
    echoLineEdit->setMaximumHeight(50);
    echoLineEdit->setMinimumHeight(50);

    echoLineEdit->setStyleSheet("background-color:white");

    layout->addWidget(echoLineEdit);

    layout->setSpacing(0);

    return layout;
}

这将产生一个如下所示的窗口:

This produces a window which looks like this:

问题是,我希望scrollArea占据整个窗口,但事实并非如此.调整窗口大小时,它也不会调整大小.

The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.

我该如何解决?

推荐答案

问题是,我希望scrollArea占据整个 窗口,但不是.调整窗口大小时,它也不会调整大小.

The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.

原因是您尚未设置任何布局来管理QScrollArea小部件本身的位置,因此它只是留给自己的设备使用(因此,它只选择默认的大小和位置)本身并保持在该大小和位置).

The reason is that you have not set any kind of layout to manage the positioning of your QScrollArea widget itself, so it is just being left to its own devices (and therefore it just chooses a default size-and-location for itself and stays at that size-and-location).

一个简单的解决方法是将这些行添加到MainWindow构造函数的底部:

A simple fix would be to add these lines to the bottom of your MainWindow constructor:

QBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->addWidget(scrollArea);

这篇关于为什么QScrollArea的大小受到限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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