我有这个:
mapdialog2.h
namespace Ui {
class MapDialog2;
}
class MapDialog2 : public QDialog
{
Q_OBJECT
public:
explicit MapDialog2(QWidget *parent = 0);
~MapDialog2();
protected:
void closeEvent(QCloseEvent * event);
void reject();
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void wheelEvent(QWheelEvent* event);
private:
Ui::MapDialog2 *ui;
QGraphicsScene *scene;
};
和此:
mapdialog2.cpp
MapDialog2::MapDialog2(QWidget *parent) :
QDialog(parent),
ui(new Ui::MapDialog2)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
scene->addPixmap(QPixmap(":/new/image/project_images/Screenshot from 2015-03-09 15:37:24.png"));
scene->activePanel();
ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
for(int i=0; i<10; i++) {
scene->addRect(50+i*30,100,50,100, QColor(0,200,0,100),QColor(20+i*20,0,0,100));
}
ui->graphicsView->setScene(scene);
}
void QGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Debug Message";
QPointF mousePos(event->buttonDownScenePos(Qt::LeftButton).x(),
event->buttonDownScenePos(Qt::LeftButton).y());
qDebug() << mousePos;
}
我想在按下鼠标左键时捕捉鼠标位置。但是从未调用过该函数。没有按下任何印刷机。为什么呢?
缩放和平移都可以正常工作,但我无法捕捉场景中的鼠标事件。我认为问题在于我应该在场景中捕获而不是QGraphisView
或QWidget
。