这次基本上没出大的错误。。根据按键向下的功能实现了一下向上的功能,还有旋转rotate()功能
myitem.h
#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsItem>
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;}
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event); //鼠标按键响应
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); //停留显示
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
private:
QColor brushColor;
};
#endif // MYITEM_H
myview.h
#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);
signals:
public slots:
};
#endif // MYVIEW_H
main.cpp
#include <QApplication>
#include "myitem.h"
#include "myview.h"
#include "myitem.cpp"
#include "myview.cpp"
#include <QTime>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
QGraphicsScene 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("../myView/11.jpg"));
view.show();
return app.exec();
}
myitem.cpp
#include "myitem.h"
#include <QPainter>
#include <QCursor>
#include <QKeyEvent>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
myItem::myItem()
{
brushColor = Qt::red;
setFlag(QGraphicsItem::ItemIsFocusable);
setFlag(QGraphicsPathItem::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)
{
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);
}
void myItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
setFocus();
setCursor(Qt::ClosedHandCursor);
}
void myItem::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Down) //x,y对应移动
{
moveBy(0,10);
}
else if(event->key() == Qt::Key_Up)
{
moveBy(0,-10);
}
}
void myItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
setCursor(Qt::OpenHandCursor);
setToolTip("I am item");
}
void myItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu menu;
QAction *moveAction = menu.addAction("move back"); //右键显示菜单
QAction *selectedAction = menu.exec(event->screenPos());
if(selectedAction == moveAction) //回到(0,0)点
{
setPos(0,0);
}
}
myview.cpp
#include "myview.h"
#include <QKeyEvent>
MyView::MyView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyView::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Plus: //+号
scale(1.2,1.2);
break;
case Qt::Key_Minus: //-号
scale(1/1.2,1/1.2);
break;
case Qt::Key_Right:
rotate(30);
break;
case Qt::Key_Left:
rotate(-30);
break;
}
QGraphicsView::keyPressEvent(event);
}