1

I have a QVBoxLayout which contains content wide enough to require a horizontal scrollbar. I want to be able to set the viewable area (effectively the equivalent of scrolling via the scrollbar) with code.

Nothing within the documentation strikes me as able to do this - and I have not been able to easily find ways to retrieve the scrollbar and modify it directly, either.

How can I modify the viewed area of a QVBoxLayout with content larger than the size of the layout?

4

1 回答 1

0

QVBoxLayout不提供任何滚动功能,因此QScrollArea可能是最简单的解决方案(如建议的那样)。它可能没有你想象的那么困难。

幸运的是,滚动区域相当容易使用。这是一个简单的示例,它使用水平滚动条将另一个小部件包装在滚动区域中:

#include <QApplication>
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QWidget>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    // Force the label to be wide
    QLabel* label = new QLabel("this is a very long label");
    label->setMinimumWidth(300);

    QWidget* widget = new QWidget();
    QVBoxLayout* layout = new QVBoxLayout(widget);
    layout->addWidget(label);

    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidget(widget);

    // Force the scroll area to be smaller
    scrollArea->resize(200, 100);
    scrollArea->show();

    app.exec();
    return 0;
}

关于滚动区域的一件棘手的事情是控制它们的大小(而不是其内容的大小)。它们定义了固定大小的提示,因此默认情况下,它们可能不会根据需要调整大小。请记住,外部滚动区域的大小提示和/或大小策略将决定它在布局中调整大小的方式。

于 2012-11-28T06:16:31.713 回答