QPixmap图像文件未出现在QGraphicsScene上

时间:2012-07-17 21:41:03

标签: c++ qt qt4 qgraphicsscene qpixmap

通过修改Qt的{​​{3}},我希望我的窗口在用户点击GraphicalScene所描述的区域时显示叠加对象(原始示例显示{{1} }})。在以下代码中我使用QPolygon。但是,单击该区域时不会显示任何内容。 QPixmap通过合理的位置点击。感谢您的帮助。

diagramScene.cpp:

mousePressEvent

diagramOverlayPixmapItem.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);
}

Diagram Scene sample类似(虽然它在#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; } 中),但没有给我一个想法。

1 个答案:

答案 0 :(得分:2)

"~"不是有效路径。这是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)
{
    ...
}