Hanu
Hanu

Reputation: 57

how to continously move series of images from left to right on screen in Qt?

I have a certain window width. I have series of images of constant width and height coming In. I want to scroll them from left to right in one window.I use QLabels to display images. I have shown below pictorially.

<--certain window width -->


| img1 |

then image 2 comes which should be displayed to the left of img1


| img2 img1 |

then image 3 comes and goes to left of img2


| img3 img2 img1 |

How do I achieve this is Qt?

Regards

Upvotes: 0

Views: 495

Answers (1)

Jeet
Jeet

Reputation: 1046

Below is a code example to add the images left side to the previous one. Code comment will explain in detail. The overall structure is ScrollArea->ScrollAreaWidget->HorizontalLayout->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);
    }

enter image description here

Check the Image color from the static array

Upvotes: 1

Related Questions