c++ - QPixmap 图像文件未出现在 QGraphicsScene 上

标签 c++ qt qt4 qgraphicsscene qpixmap

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

图表场景.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 中)但没有给我一个想法。

最佳答案

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

QDir::homePath()

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

Under non-Windows operating systems the HOME environment variable is used if it exists [...]


顺便问一下,你为什么要使用 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)
{
    ...
}

关于c++ - QPixmap 图像文件未出现在 QGraphicsScene 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11531280/

相关文章:

c++ - 将参数作为参数的回调函数传递给其他函数

c++ - 错误 : no match for call when calling a function by a thread

windows - Qt5 使用 GUI 应用程序打开控制台

c++ - Qt 4.8调整窗口全分辨率监视器

qt - 将IplImage转换为QByteArray

c++ - 成员函数声明的参数列表后面的单 & 是什么意思?

c++ - 在 vbs 中使用 C++-DLL 时出错

c++ - QtCharts添加自定义轴

qt - 如何调整 Qt 小部件的大小?

c++ - 使用 QSettings 存储 qt 应用程序的设置