image - Qt 图形 View ,显示图像! , 小部件

标签 image qt graphics view qgraphicsview

这是我的代码:

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/

相关文章:

html - 几个图像 onclick 维护其他图像

php - 通过PHP/MySQL上传图片

c++ - 复制时可执行文件以某种方式损坏

qt - Qt Installer Framework 可以返回失败退出代码吗?

c++ - Qt 未定义对 3rdparty 软件库的引用

image - 使用 MATLAB imrotate 平铺图像

ios - 代码 : compositing with alpha using core image

graphics - 如何找到所有包含图形功能的R包?

math - 不想在绘图轴上使用科学记数法

javascript - 如何使用 Three.js 定义相对于矢量路径的粒子云?