我有一定的窗口宽度。我有一系列宽度和高度恒定的图像。我想在一个窗口中从左向右滚动它们。我使用QLabel来显示图像。我已在图片中以下方显示。
< - 某个窗口宽度 - >
| img1 |
然后图像2出现在img1
的左侧| img2 img1 |
然后图像3出现并向img2的左侧
| img3 img2 img1 |
我如何实现这一目标是Qt?
此致
答案 0 :(得分:1)
下面是将图像左侧添加到上一个图像的代码示例。 代码评论将详细解释。整体结构是ScrollArea-> ScrollAreaWidget-> HorizontalLayout-> No.Of.QLable(图片显示)
//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);
}
检查静态数组中的图像颜色