1
在我的項目中,我有一個QGraphicsView
和QGraphicsScene
。我需要添加一些額外的事件到我的視圖,所以我已經subclassed QGraphicsView
。QGraphicsView子類和事件
問題是,當我設置mousePressEvent
時,我覆蓋了拖動模式ScrollHandDrag
。
我的問題是以下內容:有沒有辦法在默認QGraphicsView
答案mousePressEvent
和自定義之間切換(使用m_click
來區分不同情況)?
這裏是我的代碼:
MyQGraphicsView.h
class MyQGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
MyQGraphicsView(QGraphicsScene *scene, QWidget *parent = 0);
public slots:
// void mousePressEvent(QMouseEvent * e);
private:
QGraphicsScene *m_scene;
int m_click;
};
MyQGraphicsView.cpp
MyQGraphicsView::MyQGraphicsView(QGraphicsScene *scene, QWidget *parent) :
QGraphicsView(parent),
m_scene(scene),
m_click(0)
{
setScene(m_scene);
}
/*void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
{
double rad = 1;
QPointF pt = mapToScene(e->pos());
if (m_click)
{
m_scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0, QPen(), QBrush(Qt::SolidPattern));
m_click = 0;
}
else
{
m_click = 1;
}
}*/
基本上我想代碼做的是:當m_click=1
,畫一點,但是當m_click=0
使用ScrollHandDrag
。
我可以使兩個單獨工作,但不是在同一時間。
非常感謝,它完美的作品! – Leo