【问题标题】:display a lot of images with Qt用 Qt 显示很多图像
【发布时间】:2016-03-22 09:53:50
【问题描述】:

我想在我的 Qt 应用程序中显示很多图像,因为我创建了一个按钮,单击该按钮将访问计算机的用户并添加图像。我的问题是我不知道如何在应用程序中显示这些图像。

这是我的代码:

void Mainwindow::on_pushButton_pressed()
 {
     QStringList fileName = QFileDialog::getOpenFileNames(this,tr("Open Image"), 
                            "C:/qt-win-opensource-src-4.5.0/bin/",
                            tr("Image Files(*.png *.jpg *.bmp *.avi *.gif)"));

     iterator = new QStringListIterator(fileName);
     label = new QLabel;
     if(iterator->hasNext())
     {
         label->clear();
         label->setPixmap(QPixmap(iterator->next()));
         label->show();
     }

  }

【问题讨论】:

  • 您可以使用例如 QGraphicsView 和 QGraphicsScene 来显示您的图像 - 或者只是一个简单的小部件布局。取决于你想要什么。
  • 有足够多的例子将图像显示为标签。注意事项:默认只支持png,其他格式见stackoverflow.com/a/12800429/104774

标签: c++ qt


【解决方案1】:

您应该为所有想要显示的图像使用滚动区域。您可以根据您希望这些图像的排列方式设置布局,并使用QLabel 的实例显示它们。

iterator = new QStringListIterator(fileName);
label = new QLabel;
if(iterator->hasNext())
{
    label->clear();
    label->setPixmap(QPixmap(iterator->next()));
    ui->scrollArea->layout()->addWidget(label); // need to add a scroll area widget in your ui file
                                                // and set layout to it (horizontal, vertical, grid etc.)
}

这样应该没问题,你的标签应该可以正常显示了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2012-11-20
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多