您好,登錄后才能下訂單哦!
[+]
GraphicsView支持事件傳播體系結(jié)構(gòu),可以使圖元在場景scene中得到提高了已被的精確交互能力。圖形視圖框架中的事件都是首先由視圖進行接收,然后傳遞給場景,再由場景給相應(yīng)的圖形項。
對于鍵盤鼠標事件,scene會傳遞給獲得焦點的圖形項。如果場景沒有獲得焦點,那鍵盤事件會丟棄;如果調(diào)用場景setFocus()或者場景中的一個圖形項獲得了焦點,那么場景會自動獲得焦點;如果場景丟失了焦點(如調(diào)用clearFocus())而其中一個圖形項獲得焦點那場景會保存這個圖形項的焦點信息。
圖形項默認無法接收懸停事件,可以使用QGraphicsItem的setAcceptHoverEvents()函數(shù)使圖形項可以接收懸停事件。
(1)五個圖形項的運行圖如下圖所示。
[cpp] view plain copy
#ifndef MYSCENE_H
#define MYSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPaintEvent>
#include <QKeyEvent>
class MyScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit MyScene(QObject *parent = 0);
protected:
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
signals:
public slots:
};
#endif // MYSCENE_H
[cpp] view plain copy
#include "myscene.h"
MyScene::MyScene(QObject *parent) :
QGraphicsScene(parent)
{
clearFocus();
}
void MyScene::keyPressEvent(QKeyEvent *event)
{
qDebug("*********MyScene::keyPressEvent***************");
return QGraphicsScene::keyPressEvent(event);
}
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug("*********MyScene::mousePressEvent***************");
QGraphicsScene::mousePressEvent(event);
}
[cpp] view plain copy
#ifndef MYVIEW_H
#define MYVIEW_H
#include <QGraphicsView>
class MyView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyView(QWidget *parent = 0);
protected:
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent * event);
void mouseMoveEvent(QMouseEvent *event);
signals:
public slots:
};
#endif // MYVIEW_H
[cpp] view plain copy
#include "myview.h"
#include <QKeyEvent>
MyView::MyView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyView::keyPressEvent(QKeyEvent *event)
{
qDebug("*********MyView::keyPressEvent***************");
switch (event->key())
{
case Qt::Key_Left :
scale(1.2, 1.2);
break;
case Qt::Key_Right :
scale(1 / 1.2, 1 / 1.2);
break;
case Qt::Key_Up :
rotate(30);
break;
}
QGraphicsView::keyPressEvent(event);
}
void MyView::mousePressEvent(QMouseEvent *event)
{
qDebug("************MyView::mousePressEvent*****************");
QGraphicsView::mousePressEvent(event);
}
void MyView::paintEvent(QPaintEvent *event)
{
qDebug("************MyView::paintEvent*****************");
QGraphicsView::paintEvent(event);
}
void MyView::mouseMoveEvent(QMouseEvent *event)
{
//qDebug("************MyView::mouseMoveEvent*****************");
QGraphicsView::mouseMoveEvent(event);
}
[cpp] view plain copy
#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsItem>
#include <QGraphicsSceneEvent>
class MyItem : public QGraphicsItem
{
public:
MyItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
void setColor(const QColor &color) { brushColor = color; }
protected:
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent (QGraphicsSceneHoverEvent * event);
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
private:
QColor brushColor;
};
#endif // MYITEM_H
[cpp] view plain copy
#include "myitem.h"
#include <QPainter>
#include <QCursor>
#include <QKeyEvent>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
MyItem::MyItem()
{
brushColor = Qt::red;
setFlag(QGraphicsItem::ItemIsFocusable);
setFlag(QGraphicsItem::ItemIsMovable);
//setAcceptHoverEvents(true);
}
QRectF MyItem::boundingRect() const
{
qreal adjust = 0.5;
return QRectF(-10 - adjust, -10 - adjust,
20 + adjust, 20 + adjust);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{ qDebug("************MyItem::paint*****************");
if(hasFocus()) {
painter->setPen(QPen(QColor(255,255,255,200)));
} else {
painter->setPen(QPen(QColor(100,100,100,100)));
}
painter->setBrush(brushColor);
painter->drawRect(-10, -10, 20, 20);
}
// 鼠標按下事件處理函數(shù),設(shè)置被點擊的圖形項獲得焦點,并改變光標外觀
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug("************MyItem::mousePressEvent*****************");
setFocus();
setCursor(Qt::ClosedHandCursor);
}
// 鍵盤按下事件處理函數(shù),判斷是否是向下方向鍵,如果是,則向下移動圖形項
void MyItem::keyPressEvent(QKeyEvent *event)
{
qDebug("************MyItem::keyPressEvent*****************");
if(event->key() == Qt::Key_Down)
moveBy(0, 10);
}
// 懸停事件處理函數(shù),設(shè)置光標外觀和提示
void MyItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
qDebug("************MyItem::hoverEnterEvent*****************");
setCursor(Qt::OpenHandCursor);
setToolTip("I am item");
}
void MyItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
qDebug("************MyItem::hoverLeaveEvent*****************");
setCursor(Qt::ArrowCursor);
}
// 右鍵菜單事件處理函數(shù),為圖形項添加一個右鍵菜單
void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu menu;
QAction *moveAction = menu.addAction("move back");
QAction *actAction = menu.addAction("test");
QAction *selectedAction = menu.exec(event->screenPos());
if(selectedAction == moveAction) {
setPos(0, 0);
}
}
void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug("************MyItem::mouseMoveEvent*****************");
QGraphicsItem::mouseMoveEvent(event);
}
[cpp] view plain copy
#include <QApplication>
#include "myitem.h"
#include "myview.h"
#include "myscene.h"
#include <QTime>
int main(int argc,char* argv[ ])
{
QApplication app(argc,argv);
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
MyScene scene;
scene.setSceneRect(-200, -150, 400, 300);
for(int i = 0; i < 5; ++i) {
MyItem *item = new MyItem;
item->setColor(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
item->setPos(i * 50 - 90, -50);
scene.addItem(item);
}
MyView view;
view.setScene(&scene);
view.setBackgroundBrush(QPixmap(":/background.png"));
view.show();
return app.exec();
}
分析:keyPressEvent鍵盤按下事件由View—Scene—Item
分析:mousePressEven鼠標按下事件由View—Scene—Item
分析:事件項Item沒有獲得焦點時,mousePressEven鼠標按下事件只由View傳遞到Scene。
分析:事件項Item的懸停事件,在構(gòu)造函數(shù)中設(shè)置了setAcceptHoverEvents(true)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。