【问题标题】:QGraphicsView / QGraphicsScene image sizeQGraphicsView / QGraphicsScene 图像大小
【发布时间】:2012-10-03 20:05:06
【问题描述】:

我想用 QGraphicsScene 在 QGraphicsView 中显示图像。我的代码非常基础:

QGraphicsScene* scene = new QGraphicsScene(this);
scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif"));
ui->myGraphicsView->setScene(scene);
ui->myGraphicsView->fitInView(scene->sceneRect());

我的问题是我的图片尺寸不对。它非常小,比例错误,位于我的 GraphicsView 的中心。使用 qDebug 我知道我的图片加载成功,它的大小约为 100x800px。我的 graphicsView 较小,所以我想调整我的图片大小以使用我的 GraphicsView 大小进行调整。

在主窗口Form中设置graphicsView,在header中声明场景:“QGraphicsScene scene;”

我尝试世界上一切皆有可能(我认为),但 graphicsView 总是空白或包含图片的小版本。当我复制/粘贴一些互联网代码时,我总是遇到这个问题。我也试试这个例子:Qt GUI Development - Displaying a 2D grid using QGraphicsView,同样的问题......

也许我非常非常非常疲倦,但我真的不明白哪里出了问题。请帮帮我:)

【问题讨论】:

    标签: image qt size qgraphicsview qgraphicsscene


    【解决方案1】:

    sceneRect() 可能不是你想的那样,除非你特别设置它。此外,您在fitInview 调用中缺少aspectRatioMode,这可能会扭曲图像,有时会导致“小”外观。

    QGraphicsScene* scene = new QGraphicsScene(this);
    QGraphicsPixmapItem p = scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif"));
    ui->myGraphicsView->setScene(scene);
    ui->myGraphicsView->fitInView(p, Qt::KeepAspectRatio);
    

    【讨论】:

    • 我认为 OP 正在考虑 itemsBoundingRect() 以获取场景中所有(唯一)项目的矩形:doc.qt.digia.com/qt/qgraphicsscene.html#itemsBoundingRect
    • 我试试你的解决方案 Stephen:QPixmap pixmap(qApp->applicationDirPath() +"/france.gif"); QGraphicsPixmapItem* item = scene.addPixmap(pixmap); ui->myGraphicsView->setScene(&scene); ui->myGraphicsView->fitInView(item, Qt::KeepAspectRatio); 现在我得到了这个结果:images.meteociel.fr/im/6981/result_hnx1.PNG
    • 好吧,另一件事...我在 MainWindow 中尝试了最后一个代码。它工作得很好。我在 QWidget 中尝试 tgis 代码,该代码在单击 MainWindow 上的按钮后显示,但它不起作用:我有相同的结果:images.meteociel.fr/im/6981/result_hnx1.PNG
    【解决方案2】:

    根据我的经验,如果我们在显示场景窗口之前尝试 fitInView,则显示的图像会非常小。从技术上讲,sceneRect 的大小在实际显示之前不会按照我们想要的方式设置。因此,我们需要先让场景窗口出现,然后才能正确使用 fitInView。

    嗯,我认为解决方案在这里可用 Qt5 C++ QGraphicsView: Images don't fit view frame

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2011-11-19
      • 1970-01-01
      • 2011-06-26
      • 2011-02-16
      相关资源
      最近更新 更多