QGraphicsPixmapItem不可选 [英] QGraphicsPixmapItem not selectable

查看:217
本文介绍了QGraphicsPixmapItem不可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 QGraphicsPixmapItem QGraphicScene 上变得可选择(即可以更一般的点击方式),但不会。我实际修改 Qt 图场景示例,其中 QGraphicsItem 的子类是可选的。非常感谢您的帮助。

I want my QGraphicsPixmapItem become selectable (i.e. clickable in more general way) on QGraphicScene but it doesn't. I'm actually modifying Qt's Diagram Scene sample, where QGraphicsItem's subclass is used and is selectable. I appreciate your help.

cpp代码(部分):

cpp code (partial):

#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


推荐答案

在我的第一个注释,你调用 setAcceptedMouseButtons()方法两次。

As pointed out in my first comment, you call setAcceptedMouseButtons() method twice. While the first call actually set the correct button to be accepted, the second call make this setting lost.

要接受这个项目上的两个按钮,您必须将Qt MouseButtons标志,这样:

To accept both buttons on this item, you have to combine Qt MouseButtons flags, this way :

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

这篇关于QGraphicsPixmapItem不可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆