Search code examples
qtqlistwidget

Display big image in QListWidget


I want to use QListWidget to display big images, e.g. 3508×4961 size in pixels, each list item will display one image. The image is set in a Qlabel and the Qlabel is set into the QListWidgetItem by setItemWidget(). When my program is running, the list item cannot display the entire image because the image is too large. It only displays the upper part of an image. When I scroll down, the list item changes to the next image immediately instead of showing the lower part of the current image gradually. Does anyone know how to show the lower part of each image by scrolling?

Here is my code,

    QImage *image = new QImage("/home/sk/image1.png");
    QLabel *label = new QLabel;
    label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
    QListWidgetItem *ite = new QListWidgetItem;
    auto size = label->sizeHint();
    ite->setSizeHint(label->sizeHint());
    size = ite->sizeHint();
    ui->listWidget->addItem(ite);
    ui->listWidget->setItemWidget(ite, label);

    image = new QImage("/home/sk/image2.png");
    label = new QLabel;
    label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
    ite = new QListWidgetItem;
    ite->setSizeHint(label->sizeHint());
    ui->listWidget->addItem(ite);
    ui->listWidget->setItemWidget(ite, label);

    image = new QImage("/home/sk/image3.png");
    label = new QLabel;
    label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
    ite = new QListWidgetItem;
    ite->setSizeHint(label->sizeHint());
    ui->listWidget->addItem(ite);
    ui->listWidget->setItemWidget(ite, label);

Solution

  • have you tried changing the scroll mode to ScrollPerPixel

    setVerticalScrollMode

    and

    setHorizontalScrollMode

        ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
    

    and in order to change the scroll step

        ui->listWidget->verticalScrollBar()->setSingleStep(10);
        ui->listWidget->verticalScrollBar()->setPageStep(20);
    

    these values will be used for scrolling