首页 文章

滚动区域中的自定义小部件不符合最低要求?

提问于
浏览
0

我越来越接近让QScrollArea工作,但它仍在缩小我的自定义小部件,因为它们被添加 . 一切都在流畅地调整大小,如果滚动区域太小,则会出现滚动条,因此它显然有一些最小尺寸的概念 .

首先,在滚动区域中有两个自定义小部件,您可以看到一些缩小:

screenshot - widgets are being shrunk

这是关键点下方的同一窗口 . 文本现已完全消失,但它不会缩小QLineEdit,因此它最终添加了一个滚动条 . (滚动区域为蓝色背景,内容小部件为紫色)

enter image description here

我从设计开始,但滚动区域小部件下面的所有内容都在代码中,因为我无法使用设计使垂直布局正常工作 .

这是起点:

screenshot of design

StackWidget中有一个页面,在垂直布局中有两个元素 . 滚动区域有一个QWidget . MainWindow的构造函数定义了一个垂直布局,将其分配给成员 scrollWidgetLayout ,并将该布局提供给滚动区域的小部件:

scrollWidgetLayout = new QVBoxLayout(ui->scrollAreaWidgetContents);
ui->scrollAreaWidgetContents->setLayout(scrollWidgetLayout);

应用程序从堆栈小部件的第一页开始,用户登录,应用程序运行以从服务器获取记录 . 每条记录都变成了一个小部件:

RecordFolderWidget::RecordFolderWidget(Record *r, QWidget *parent) : QWidget(parent)
{
    record = r;
    //setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);

    QGridLayout *layout = new QGridLayout();
    pathLineEdit = new QLineEdit();
    finderButton = new QPushButton("...");
    QLabel *nameLabel = new QLabel(record->name);
    layout->setSpacing(5);
    layout->setMargin(3);
    layout->addWidget(nameLabel, 0, 0, 0, 1, Qt::AlignCenter);
    layout->addWidget(pathLineEdit, 1, 0);
    layout->addWidget(finderButton, 1, 1);
    setLayout(layout);
    //setMinimumHeight(sizeHint().height());
}

请注意,有一些注释掉的行,这些是我一直在玩的东西,试图让它工作 . sizeHint,btw似乎是正确的,并且不会改变 .

一旦创建了Widget,它就会被添加到滚动区域的小部件中:

RecordFolderWidget *rf = new RecordFolderWidget(record);
    rf->setParent(ui->scrollAreaWidgetContents);

    scrollWidgetLayout->addWidget(rf);

我在这里尝试也调整滚动区域内容的大小,没有运气:

ui->scrollAreaWidgetContents->resize(rfSize.width(), rfSize.height() * records.count());

其中rfSize在创建后从自定义窗口小部件的sizeHint中拉出,并且在循环之后调用此行以创建/添加所有窗口小部件 .

除了setMinimumHeight并在上面调整大小之外,我已经尝试将scrollAreaWidgetContents的SizePolicy从首选更改为扩展到minimumexpanding并且没有看到任何差异 . 我敢肯定我错过了一些微不足道的东西,但却找不到它 .

1 回答

  • 1

    我在你的代码中看到的问题是在添加QLabel时你将rowSpan设置为0,我已经改变了它,我可以正确地观察它 . 在下一部分中,我将展示我的测试代码和结果:

    QVBoxLayout *scrollWidgetLayout = new QVBoxLayout(ui->scrollAreaWidgetContents);
    
    for(int i=0; i<10; i++){
        QWidget *widget = new QWidget;
        QGridLayout *layout = new QGridLayout(widget);
        QLineEdit *pathLineEdit = new QLineEdit;
        QPushButton *finderButton = new QPushButton("...");
        QLabel *nameLabel = new QLabel(QString("name %1").arg(i));
        layout->setSpacing(5);
        layout->setMargin(3);
        layout->addWidget(nameLabel, 0, 0, 1, 1, Qt::AlignCenter);
        layout->addWidget(pathLineEdit, 1, 0);
        layout->addWidget(finderButton, 1, 1);
    
        scrollWidgetLayout->addWidget(widget);
    }
    

    enter image description here

相关问题