【问题标题】:how to continously move series of images from left to right on screen in Qt?如何在Qt的屏幕上从左到右连续移动一系列图像?
【发布时间】:2016-02-04 15:24:07
【问题描述】:

我有一定的窗口宽度。我有一系列恒定宽度和高度的图像进来。我想在一个窗口中从左到右滚动它们。我使用 QLabels 来显示图像。我已经在下面进行了图示。


|图片1 |

然后图片2来了,应该显示在img1的左边


| img2 img1 |

然后图像 3 出现在 img2 的左侧


| img3 img2 img1 |

Qt 是如何实现的?

问候

【问题讨论】:

  • 我可以帮助你。但是想了解更多关于“在一个窗口中从左到右滚动它们”的信息。这意味着,它被限制从右到左滚动?能不能写的详细一点。
  • @Jeet:谢谢 Jeet。 “在一个窗口中从左到右滚动它们”意味着 img1 应该继续向右移动,这样做会将 img2 容纳在其左侧,然后 img1 和 img2 一起移动到右侧并容纳 img3。这对吉特有帮助吗?
  • 据我了解,我已经给出了答案。希望有所帮助。

标签: qt


【解决方案1】:

下面是一个将左侧图像添加到前一个图像的代码示例。 代码注释会详细解释。整体结构为ScrollArea->ScrollAreaWidget->Horizo​​ntalLayout->No.Of.QLable(Image Display)

//scroll area
QScrollArea *scrollArea;
    //widget inside scroll area
    QWidget *scrollAreaWidgetContents;
    //Horizontal Box Layout
    QHBoxLayout *horizontalLayout;
    //Image loaded to find the width and height to resize the scroll area.   
    QImage img(QString::fromUtf8("../../../Downloads/red_star.png"));

    scrollArea = new QScrollArea(this->centralWidget());
    //Adding the size of the scrollbar with the image width
    int intWidth=img.width()*2.5+qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent)*2;
    //Adding the size of the scrollbar with the image Height
    int intHeight=img.height()+qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent)*2;

    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //Sizing the scroll area and the inside widget  
    scrollArea->setGeometry(QRect(0, 0,intWidth ,intHeight));
    scrollArea->setWidgetResizable(true);
    scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setGeometry(QRect(0, 0, intWidth, intHeight));
    Making the main window width to the scroll area width
    this->setFixedWidth(scrollArea->width());
    horizontalLayout = new QHBoxLayout(scrollAreaWidgetContents);
    scrollArea->setWidget(scrollAreaWidgetContents);
    //Image static array
    QString strImages[]= {"../../../Downloads/red_star.png","../../../Downloads/green_star.png","../../../Downloads/yellow_star.png"};
    //Add the image in the left side of the previous one (Note the color)
    for(int iCount=0; iCount<3; iCount++)
    {
        QLabel *label = new QLabel(QStringLiteral("label"),scrollAreaWidgetContents);
        label->setPixmap(QPixmap(strImages[iCount]));
        //Adding the new image to the left of previous image
        horizontalLayout->insertWidget(0,label);
    }

检查静态数组中的图像颜色

【讨论】:

  • :@Jeet。我有固定大小的窗口,就像一个表演舞台。你的新(橙色)img1 演员来了,应该走到窗口的尽头,然后消失。我不应该再看到它。就像一条带有图像的丝带。您的代码为我的应用程序开发提供了新的功能输入。感谢 Jeet 抽出宝贵时间。现在是否可以更改您的代码以使图像消失而无需滚动条。
  • 我显示了滚动条以供您理解。如果您不想要,请添加此行“scrollArea->setHorizo​​ntalScrollBarPolicy(Qt::ScrollBarAlwaysOff);”。一旦图像从视图中消失,然后从 QLabel 中释放它。还有 QLable。尝试这个。如果您需要更改代码,请告诉我。
  • 我有一个QMainWindow。里面是QWidget。里面是QGridLayout。在 QGridLayout 中,我有所有 QLabels 来显示图像。我现在如何实施您的概念?谢谢吉特。
  • 是的,正确。所以现在您可以更改上面给出的代码,适合您的小部件(答案)并完成逻辑。
  • 这是我想出的代码。请做出必要的假设。 QScrollArea 滚动 = 新 QScrollArea();滚动->setGeometry(...理解..);滚动区域->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollArea->setWidget(scrollAreaWidgetContents); QLabel *label = new QLabel(scrollAreaWidgetContents);标签->setPixmap(QPixmap(..some image....)); ui->gridlayout->addWidget(label,0,0,0);网格布局没有向左显示的选项。我该如何克服这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多