2014-08-31 31 views
0

我有这样的程序:在我的小工具我有qpushbuttons,qslider和qgraphicsview。我在qgraphicsview中放置了qgraphicsscene。问题在于,当我点击QGraphicsscene并想绘制一个点时,点击它就会出现位移。当我点击QGraphicsScene的中心时,没有任何问题。位移在QGraphicsScene

if (mouseEvent->button() == Qt::LeftButton) 
{ 
    QPointF pos = mouseEvent->scenePos(); 
    addEllipse(pos.x(), pos.y(), 5, 5, QPen(QColor(Qt::green)), QBrush(QColor(Qt::red))); 
} 

还有一个屏幕。 所以,问题是如何避免这种场景的移动。 Screen

在我点击了在右下角的屏幕,但点的中心绘制

回答

1

试试这个。在我的电脑工作没有位移

。* H

#ifndef GRAPHICSSCENE_H 
#define GRAPHICSSCENE_H 

#include <QGraphicsScene> 
#include <QMouseEvent> 
class GraphicsScene : public QGraphicsScene 
{ 
    Q_OBJECT 
public: 
    explicit GraphicsScene(QObject *parent = 0); 

signals: 

protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); 
public slots: 

}; 

#endif // GRAPHICSSCENE_H 

*的.cpp

#include "graphicsscene.h" 
#include <QDebug> 
#include <QGraphicsSceneMouseEvent> 

GraphicsScene::GraphicsScene(QObject *parent) : 
    QGraphicsScene(parent) 
{ 
} 

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) 
{ 
    qDebug() << "in"; 
    if (mouseEvent->button() == Qt::LeftButton) 
    { 
     QPointF pos = mouseEvent->scenePos(); 
     addEllipse(pos.x(), pos.y(), 5, 5, QPen(QColor(Qt::green)), QBrush(QColor(Qt::red))); 

    } 
} 

使用

GraphicsScene *scene = new GraphicsScene(this); 
ui->graphicsView->setSceneRect(50,50,50,50);//for example 
ui->graphicsView->setScene(scene); 
ui->graphicsView->show(); 
0

视图 - > setSceneRect(0,0,640, 480); 已帮助我

+0

我告诉过你同样的解决方案。 – Chernobyl 2014-08-31 16:39:28

+0

有没有这个,当我张贴回答 ui-> graphicsView-> setSceneRect(50,50,50,50);//例如 – OneOne 2014-08-31 16:44:34

+0

我明白了,我真的这样编辑一分钟后 – Chernobyl 2014-08-31 16:49:31