如何在Qt中为水平布局添加滚动区域?

Dev*_*Dev 0 qt qt-creator qt-designer

如何在Qt中为水平布局添加滚动区域?我有一个我最近开发的应用程序的水平布局.水平布局包含一系列标签小部件,因此我需要将它们放在滚动区域中,以便我们可以相应地滚动它.例如,它就像滚动区域内列出的购物车网站中的最新产品一样.如何为此定义滚动区域?以下是我使用的示例代码:

// val is a list containing some names
for (int i = 0; i < val.size(); ++i) {
    if (session->getUserName()!=val[i]) {
    {
        QLabel *label4;
        label4=new QLabel();
        label4->width();
        label4->height();
        QPainter painter(this);
        painter.setPen(Qt::blue);
        painter.drawEllipse(0, 0, 20, 20);
        QPixmap icon(QString::fromUtf8(":/new/prefix1/singleuser.png"));

        QImage fixedImage(20, 20, QImage::Format_ARGB32_Premultiplied);
        fixedImage.fill(0);  // Make sure you don't have garbage in there

        QPainter imgPainter(&fixedImage);
        QPainterPath clip;
        clip.addEllipse(0, 0, 20, 20);  // this is the shape we want to clip to
        imgPainter.setClipPath(clip);
        imgPainter.drawPixmap(0, 0, 20, 20, icon);
        imgPainter.end();

        label4->setPixmap(QPixmap::fromImage(fixedImage));
        ui->horizontalLayout->addWidget(label4);
        listforgroup<<label4;
        QLabel *label;
        label=new QLabel(val[i]);

        listforgroup<<label;

        ui->horizontalLayout->addWidget(label);
    }
Run Code Online (Sandbox Code Playgroud)

我需要添加滚动区域horizontalLayout.如何添加?

Jac*_*ieg 5

你不希望添加QScrollAreaQHBoxLayout(水平布局)......你想要的是一个QScrollArea 带有QHBoxLayout对这样的:

在此输入图像描述