Qt
通过修改图表场景示例,我希望我的窗口在用户单击所描述的区域时显示覆盖对象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);
}
图OverlayPixmapItem.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;
}
这个线程是相似的(虽然它在python
)但没有给我一个想法。