首页 > 解决方案 > 如何在移动滑块的同时连续加载和显示多个图像?

问题描述

我正在使用 c++ 在 Qt 中的图像查看器中创建一个函数,其中可以在移动滑块的同时加载和显示多个图像。我可以参考任何想法或代码参考吗?谢谢你。

    void MainWindow::on_btn_image_clicked()
    {
        qDebug()<<"clicked.....";
        QStringList filename = QFileDialog::getOpenFileNames(this, tr("Browse Image"), "", tr("Images(*.png *.jpg *.bmp *.gif)"));

        if (!filename.isEmpty())
        {
            for(int i=0; i<filename.length(); i++)
            {
                QString str = filename.at(i) ;
                qDebug()<<"str========>>>>>"<<str;

                QImage image1(str);

                QByteArray bytes;
                QBuffer buffer(&bytes);
                buffer.open(QIODevice::WriteOnly);
                image1.save(&buffer,"");
                buffer.close();

                unsigned char *data_image = (unsigned char *)malloc(bytes.size());
                memcpy(data_image, reinterpret_cast<unsigned char *>(bytes.data()), (unsigned int)bytes.size());



                auto sigMap = new QSignalMapper(this);

                horizontalSlider[i] = new QSlider(this);
                connect(horizontalSlider[i], SIGNAL(valueChanged(int)), sigMap, SLOT(map()));

                sigMap->setMapping(horizontalSlider[i], i);

                connect(sigMap, SIGNAL(mapped(int)), this, SLOT(slider_x(int)));


                int h=image1.height();
                int w=image1.width();

                QImage image2(str);
                image2= QImage(data_image, h, w, QImage::Format_Indexed8);

                if (image2.height()>=image2.width())
                {
                     image = image2.scaledToHeight(ui->graphicsView->height(), Qt::SmoothTransformation);
                }

                else
                {
                     image = image2.scaledToWidth(ui->graphicsView->width(), Qt::SmoothTransformation);
                }

                item = new QGraphicsPixmapItem(QPixmap::fromImage(image));

                scene[i] = new QGraphicsScene(this);
                ui->graphicsView->setScene(scene[i]);
                scene[i]->addItem(item);

            }
        }

     }

    void MainWindow::slider_x(int i)
    {
        int value = horizontalSlider[i]->value();
    }

我正在尝试将图像转换为字节数组并将数组自动连接到滑块。概念或代码有什么问题吗?

标签: c++qt

解决方案


我的方法是在一个 flickable 中加载布局中的所有图像,并将 flickable 与滑块连接起来。您可以选择以下图片在屏幕外加载的方式的布局,这样您就可以在窗口/屏幕上移动一个图像栏。但是,当图片数量变大时,这可能会变得非常耗费资源。如果您正在处理大量图像,您应该按需加载它们,并在它们离开屏幕时卸载它们。但是使用这种方法,我不确定如何处理项目/对象。


推荐阅读