QGraphicsPixmapItem不可选(QGraphicsPixmapItem not sel

2019-09-19 19:59发布

我希望我的QGraphicsPixmapItem变为可选(即点击在更一般的方式)在QGraphicScene但事实并非如此。 实际上,我修改Qt的图表场景样品 ,其中QGraphicsItem的子类被使用并且是可选择的。 我感谢您的帮助。

CPP码(部分):

#include <iostream>
#include <QtGui>    
#include "overlayPixmapItem.h"

OverlayPixmapItem::OverlayPixmapItem(DiagramType diagramType, QMenu *contextMenu,
                                                   QPixmap img, QGraphicsItem *parent,
                                                   QGraphicsScene *scene)
    : QGraphicsPixmapItem(img, parent), QObject()
{
    myDiagramType = diagramType;
    myContextMenu = contextMenu;

    this->setAcceptsHoverEvents(true);
    this->setAcceptHoverEvents(true); //
    this->setAcceptedMouseButtons(Qt::LeftButton);
    this->setAcceptedMouseButtons(Qt::RightButton);
    this->acceptDrops();

    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
:

报头(部分):

#ifndef OVERLAYPIXMAPITEM_H
#define OVERLAYPIXMAPITEM_H

#include <QGraphicsPixmapItem>
#include <QList>
#include <QObject>

class QPixmap;
class QGraphicsItem;
class QGraphicsScene;
class QTextEdit;
class QGraphicsSceneMouseEvent;
class QMenu;
class QGraphicsSceneContextMenuEvent;
class QPainter;
class QStyleOptionGraphicsItem;
class QWidget;
class QPolygonF;

class OverlayPixmapItem : public QObject, public QGraphicsPixmapItem
{
        Q_OBJECT

    public:
        enum { Type = UserType + 15 };
        enum DiagramType { Crosshair };

        OverlayPixmapItem(DiagramType diagramType, QMenu *contextMenu,
                                 QPixmap img, QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);

        DiagramType diagramType() const { return myDiagramType; }
        QPolygonF polygon() const { return myPolygon; }
        int type() const { return Type;}

    protected:
        void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
        QVariant itemChange(GraphicsItemChange change, const QVariant &value);
        void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
        void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );

    public: signals:
        void mouseHoveredOnElem(OverlayPixmapItem *i);
        void mouseHoveredOutOfElem(OverlayPixmapItem *i);

    private:
        DiagramType myDiagramType;
        QPolygonF myPolygon;
        QMenu *myContextMenu;
};
#endif // OVERLAYPIXMAPITEM_H

Answer 1:

正如我的第一个评论指出的那样,你叫setAcceptedMouseButtons()方法的两倍。 虽然第一次调用实际设置正确的按钮被接受,第二个电话进行此设置丢失。

若要接受关于这个项目的两个按钮,你必须Qt的MouseButtons标志结合使用,这种方式:

item->setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton) ;


文章来源: QGraphicsPixmapItem not selectable