这是我的代码:
void MainWindow::on_actionOpen_Image_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open Image File",QDir::currentPath());
if(!fileName.isEmpty())
{
QImage image(fileName);
if(image.isNull())
{
QMessageBox::information(this,"Image Viewer","Error Displaying image");
return;
}
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap::fromImage(image));
scene.addItem(&item);
view.show();
}
}
我想显示文件中的图像,代码工作正常,但图像消失得非常快。
如何暂停图像屏幕?
以及如何在“graphicsView”小部件中加载图像?
我的代码:
void MainWindow::on_actionOpen_Image_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open Image File",QDir::currentPath());
if(!fileName.isEmpty())
{
QImage image(fileName);
if(image.isNull())
{
QMessageBox::information(this,"Image Viewer","Error Displaying image");
return;
}
QGraphicsScene scene;
QGraphicsPixmapItem item(QPixmap::fromImage(image));
scene.addItem(&item);
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
}
}
它不起作用。
如何解决这个问题?
最佳答案
您需要在堆上创建所有对象,否则它们会在超出范围时被删除:
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsView* view = new QGraphicsView(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
view->show();
您的第二个问题可能与此相关 -
scene
分配给 ui->graphicsView
但它会立即被删除,因此再次在堆上创建所有对象。
关于image - Qt 图形 View ,显示图像! , 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7138113/