Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, July 2007
Problem with positioning of QGraphicsLineItem


Message 1 in thread

Hi,
 
I have a QGraphicsLineItem that I want should move only along
x-direction.
To do so I override the mouseMoveEvent handler and do a setPos() to
position it correctly.
 
This does not give the desired result. It moves along x-direction all
right, but there it is a lot of flicker and positioning issues.
 
I am attaching a small compilable piece of code to show the problem.
 
Please let me know if what I am doing is correct, or if there is a
better way to do this.
 
Thnx
Prateek
 
//Header File
 
 
#pragma once
#include <QGraphicsLineItem>
#include <QGraphicsSceneMouseEvent>
#include <QLineF>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsView>
 
class Arrow : public QGraphicsLineItem
{
protected:
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
public:
Arrow( QLineF & line, QGraphicsItem * parent = 0, QGraphicsScene * scene
= 0 );
};
 
//cpp file
 
 
#include <qapplication.h>
#include "myview.h"
 
Arrow::Arrow(QLineF &line, QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsLineItem(line, parent, scene)
{
    setFlag(QGraphicsItem::ItemIsSelectable);
    setFlag(QGraphicsItem::ItemIsMovable);
}
 
void Arrow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    setPos(event->pos());
}
 
 
int main(int argc, char** argv)
{
    QApplication app(argc,argv);
    QGraphicsScene *scene = new QGraphicsScene();
    scene->setSceneRect(QRectF(0,0,500,500));
    QGraphicsView m_view(scene);
    m_view.show();
 
    Arrow * arrow = new Arrow(QLineF(0,0,100,100), NULL, scene);
    arrow->setPos(50,50);
 
    return app.exec();
}
 

Message 2 in thread

Hi,

Change event->pos() to event->scenePos() in mouseMoveEvent.

Sincerely,

Bo Thorsen.
Thorsen Consulting - http://www.qt-experts.com

Prateek Tiwari skrev:
> Hi,
>  
> I have a QGraphicsLineItem that I want should move only along x-direction.
> To do so I override the mouseMoveEvent handler and do a setPos() to 
> position it correctly.
>  
> This does not give the desired result. It moves along x-direction all 
> right, but there it is a lot of flicker and positioning issues.
>  
> I am attaching a small compilable piece of code to show the problem.
>  
> Please let me know if what I am doing is correct, or if there is a 
> better way to do this.
>  
> Thnx
> Prateek
>  
> //Header File
>  
>  
> #pragma once
> #include <QGraphicsLineItem>
> #include <QGraphicsSceneMouseEvent>
> #include <QLineF>
> #include <QGraphicsScene>
> #include <QGraphicsItem>
> #include <QGraphicsView>
>  
> class Arrow : public QGraphicsLineItem
> {
> protected:
> virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
> public:
> Arrow( QLineF & line, QGraphicsItem * parent = 0, QGraphicsScene * 
> scene = 0 );
> };
>  
> //cpp file
>  
>  
> #include <qapplication.h>
> #include "myview.h"
>  
> Arrow::Arrow(QLineF &line, QGraphicsItem *parent, QGraphicsScene 
> *scene) : QGraphicsLineItem(line, parent, scene)
> {
>     setFlag(QGraphicsItem::ItemIsSelectable);
>     setFlag(QGraphicsItem::ItemIsMovable);
> }
>  
> void Arrow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
> {
>     setPos(event->pos());
> }
>  
>  
> int main(int argc, char** argv)
> {
>     QApplication app(argc,argv);
>     QGraphicsScene *scene = new QGraphicsScene();
>     scene->setSceneRect(QRectF(0,0,500,500));
>     QGraphicsView m_view(scene);
>     m_view.show();
>  
>     Arrow * arrow = new Arrow(QLineF(0,0,100,100), NULL, scene);
>     arrow->setPos(50,50);
>  
>     return app.exec();
> }
>  

--
 [ signature omitted ] 

Message 3 in thread

Works perfectly now !

Thanx a lot 
Prateek

-----Original Message-----
From: Bo Thorsen [mailto:bo@xxxxxxxxxxxxxxxxxxxxx] 
Sent: Thursday, July 19, 2007 2:45 PM
To: Prateek Tiwari
Cc: qt-interest@xxxxxxxxxxxxx
Subject: Re: Problem with positioning of QGraphicsLineItem

Hi,

Change event->pos() to event->scenePos() in mouseMoveEvent.

Sincerely,

Bo Thorsen.
Thorsen Consulting - http://www.qt-experts.com

Prateek Tiwari skrev:
> Hi,
>  
> I have a QGraphicsLineItem that I want should move only along
x-direction.
> To do so I override the mouseMoveEvent handler and do a setPos() to 
> position it correctly.
>  
> This does not give the desired result. It moves along x-direction all 
> right, but there it is a lot of flicker and positioning issues.
>  
> I am attaching a small compilable piece of code to show the problem.
>  
> Please let me know if what I am doing is correct, or if there is a 
> better way to do this.
>  
> Thnx
> Prateek
>  
> //Header File
>  
>  
> #pragma once
> #include <QGraphicsLineItem>
> #include <QGraphicsSceneMouseEvent>
> #include <QLineF>
> #include <QGraphicsScene>
> #include <QGraphicsItem>
> #include <QGraphicsView>
>  
> class Arrow : public QGraphicsLineItem {
> protected:
> virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
> public:
> Arrow( QLineF & line, QGraphicsItem * parent = 0, QGraphicsScene * 
> scene = 0 ); };
>  
> //cpp file
>  
>  
> #include <qapplication.h>
> #include "myview.h"
>  
> Arrow::Arrow(QLineF &line, QGraphicsItem *parent, QGraphicsScene
> *scene) : QGraphicsLineItem(line, parent, scene) {
>     setFlag(QGraphicsItem::ItemIsSelectable);
>     setFlag(QGraphicsItem::ItemIsMovable);
> }
>  
> void Arrow::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
>     setPos(event->pos());
> }
>  
>  
> int main(int argc, char** argv)
> {
>     QApplication app(argc,argv);
>     QGraphicsScene *scene = new QGraphicsScene();
>     scene->setSceneRect(QRectF(0,0,500,500));
>     QGraphicsView m_view(scene);
>     m_view.show();
>  
>     Arrow * arrow = new Arrow(QLineF(0,0,100,100), NULL, scene);
>     arrow->setPos(50,50);
>  
>     return app.exec();
> }
>  

--
 [ signature omitted ]