繁体   English   中英

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

[英]How to add a Scroll area for a horizontal layout in Qt?

如何在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);
    }

我需要为horizontalLayout添加滚动区域。 如何添加呢?

您不想为QHBoxLayout (水平布局)添加QScrollArea ...您想要的是带有 QHBoxLayoutQScrollArea ,如下所示:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM