【问题标题】:QPixmap image file not appear on QGraphicsSceneQPixmap 图像文件未出现在 QGraphicsScene 上
【发布时间】:2012-07-17 21:41:02
【问题描述】:

通过修改QtDiagram Scene sample,我希望我的窗口在用户单击GraphicalScene 描述的区域时显示覆盖对象(原始示例显示QPolygon)。在下面的代码中,我使用了QPixmap。但是,单击该区域时不会出现任何内容。 mousePressEvent 通过点击的合理位置。感谢您的帮助。

diagramScene.cpp:

#include <iostream>
#include <QtGui>
#include <QPixmap>

#include "pkg/diagramscene.h"
#include "pkg/arrow.h"

DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent)
    : QGraphicsScene(parent)
{
    myItemMenu = itemMenu;
    myMode = MoveItem;
    myItemType = DiagramOverlayPixmapItem::Overlay;
}

void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mouseEvent->button() != Qt::LeftButton) return;
    DiagramOverlayPixmapItem *item;
    switch (this->myMode) {
        case InsertItem: {
            const std::string tmpImgPathBase = "~/images/";
            std::string overlayObj = tmpImgPathBase + "overlayObj.png";
            QPixmap *img = new QPixmap(QString(overlayObj.c_str()));

            item = new DiagramOverlayPixmapItem(myItemType, myItemMenu, img);
            item->setPos(mouseEvent->scenePos());
            this->addPixmap(item->pixmap());  // QGraphicsScene::addPixmap.
            this->update();     // Expecting these parts would do the work..
        }
            break;
        default:
            ;
    }
    QGraphicsScene::mousePressEvent(mouseEvent);
}

diagramOverlayPixmapItem.cpp:

#include <iostream>
#include <QtGui>    
#include "pkg/arrow.h"
#include "pkg/diagramOverlayPixmapItem.h"

DiagramOverlayPixmapItem::DiagramOverlayPixmapItem(DiagramType diagramType, QMenu *contextMenu, QPixmap *img, QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPixmapItem(*img, parent)
{
    myDiagramType = diagramType;
    myContextMenu = contextMenu;
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}

void DiagramOverlayPixmapItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    scene()->clearSelection();
    setSelected(true);
    myContextMenu->exec(event->screenPos());
}

QVariant DiagramOverlayPixmapItem::itemChange(GraphicsItemChange change,
                                              const QVariant &value)
{
    if (change == QGraphicsItem::ItemPositionChange) {
        foreach (Arrow *arrow, arrows) {
            arrow->updatePosition();
        }
    }
    return value;
}

This thread 类似(虽然它在 python 中)但不给我一个想法。

【问题讨论】:

  • 你检查文件是否打开成功?
  • @Blood 如果您的意思是“打开”为“编译”或“运行”,是的。它运行,打开窗口,QGraphicsScene 显示的区域是可点击的,并返回点击位置。
  • 不,我需要你的 QPixmap - QPixmap *img = new QPixmap(QString(overlayObj.c_str()));
  • @Blood 文件的路径是正确的(我从另一个有效的 .cpp 代码复制并粘贴),但我不确定这个特定的程序是否成功打开给定的文件,因为我没有看到它我的 GUI(无论如何要验证 QPixmap 是否成功打开它?)。
  • @Blood 你的意思是:!img-&gt;isNull(),因为img在新操作后不能为NULL。

标签: c++ qt qt4 qgraphicsscene qpixmap


【解决方案1】:

"~" 不是有效路径。这是shell expansion 将其替换为:

QDir::homePath()

返回当前用户主目录的QStringThe documentation 说:

在非 Windows 操作系统下,使用 HOME 环境变量(如果存在)[...]


顺便问一下,你为什么用std::string?您应该在 Qt 应用程序中使用 QString

您还可以通过以下方式使用QDir 路径构建机制:

QDir tmpImgPathBase = QDir::home(); //home() returns a QDir, homePath() a QString
tmoImgPathBase.cd("images"); //navigate to relative directory
QString overlayObj = tmoImgPathBase.filePath("overlayObj.png"); //file in direct.
QPixmap *img = new QPixmap(overlayObj);

请注意,您也不应该在 C++ 中过度使用指针。 pixmap 不需要是一个指针(你永远不会在这里删除它,这会导致内存泄漏!):

QPixmap img = QPixmap(overlayObj);
item = new DiagramOverlayPixmapItem(myItemType, myItemMenu, img);

// I removed the asterisk at both the argument img and the
// forwarding to the constructor of QGraphicsPixmapItem.
DiagramOverlayPixmapItem::DiagramOverlayPixmapItem(DiagramType diagramType, QMenu *contextMenu, QPixmap img, QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPixmapItem(img, parent)
{
    ...
}

【讨论】:

  • 应用您的所有建议(即使用QDir,用QString 替换std::string,删除不必要的指针)已解决。正如你所建议的,也许使用“~”作为路径是错误的。谢谢!
  • 当然,使用“~”是错误,其余的只是“样式”(但在大多数情况下,这样的内存泄漏可能很危险)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2015-08-22
  • 2011-11-19
相关资源
最近更新 更多