我正在使用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();
}
我正在尝试将图像转换为字节数组并将数组自动连接到滑块。概念或代码是否有问题?
答案 0 :(得分:0)
我的方法是将所有图片加载到可滑动弹片中的布局中,并将可滑动弹片与滑块连接起来。您可以选择在屏幕外加载以下图片的方式来选择布局,这样就可以在整个窗口/屏幕上移动一系列图片。但是,当图片数量变大时,这可能变得非常耗费资源。如果要处理大量图像,则应按需加载它们,并在屏幕外时卸载它们。但是用这种方法我不确定如何处理项目/对象。