2014-08-31 33 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