Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -
i new qt. trying catch mouse press , release in qgraphicsview
area , draw line. this, extended qgraphicsview
, called myqgraphicsview
. seems scene not attached since added "hello world" , never appears in view. here code extended class:
class myqgraphicsview : public qgraphicsview { q_object public: myqgraphicsview(qgraphicsscene *scene, qwidget *parent = q_nullptr) : qgraphicsview(scene,parent){ // qgraphicsview::qgraphicsview(scene,parent); } qpoint *pointptr, *startpos,*endpos; qgraphicsview *cv;//maa qgraphicsscene *c; qgraphicsscene *sceneptr; // maa protected: void paintevent(qpaintevent *event) { show(); } void mousepressevent(qmouseevent *event) { // pos wrt widget. global wer global screen qdebug()<<" mouse pressed @ "<<qwidget::mapfromglobal(event->globalpos()); //globalx(); startpos = new qpoint; startpos->setx(qwidget::mapfromglobal(event->globalpos()).x()); startpos->sety(qwidget::mapfromglobal(event->globalpos()).y()); } void mousereleaseevent(qmouseevent *event) { qdebug()<<" mouse relase pressed @ x="<<event->globalpos().x()<<" y ="<<event->globalpos().y(); endpos = new qpoint; endpos->setx(qwidget::mapfromglobal(event->globalpos()).x()); endpos->sety(qwidget::mapfromglobal(event->globalpos()).y()); c->addline(0,0,50,50); update(); } void mousemoveevent ( qmouseevent * event ) { //show x , y coordinate values of mouse cursor here qdebug()<<"x:"<<qstring::number(event->x())<<"-- y:"<<qstring::number(event->y()); endpos = new qpoint; } }
it seems mouse gets detected , right methods called. however, neither "hello world" nor line gets drawn in view. tried cv1->setscene
on view , did not either.
so declared 2nd view pointer called cv2
, using qgraphicsview
. information seems work fine there , gets drown. read through posting , seems suggestion of setting event filters added both views (cv1
, cv2
). event filter returns false
events ( should default behavior documentation). did not anything. here code snippet in great.cpp cv1
, cv2
. notice in cv1
(mymyqgraphicsview
instance), created pointer scene (called c
) , gets assigned c1
can addline
it.
c1 = new qgraphicsscene(0,0,500,300,this); text= c1->addtext("hello, world!"); text->setflag(qgraphicsitem::itemismovable); text->setvisible(true); cv1 = new myqgraphicsview(c1,this); cv2= new qgraphicsview(c1,this); keypresseater *keypresseaterptr = new keypresseater(this); cv1->installeventfilter(keypresseaterptr); cv2->installeventfilter(keypresseaterptr); cv1->c =c1; layout = new qgridlayout; layout->addwidget(toplabel,0,0); layout->addwidget(wellcoord,1,0); layout->addwidget(cv1,1,1,1,-1); <-view added great class layout->addwidget(timestepgroup,2,0); setlayout(layout);
finally, here great.h:
myqgraphicsview *cv1;//maa qgraphicsview *cv2;//maa qgraphicsscene *c1; // maa public: void paintevent(qpaintevent *event) { // maa cv2->show(); } protected: void mousepressevent(qmouseevent *event) { qdebug()<<"gridwell mouse pressed @ "<<qwidget::mapfromglobal(event->globalpos()); //globalx(); startpos = new qpoint; startpos->setx(qwidget::mapfromglobal(event->globalpos()).x()); startpos->sety(qwidget::mapfromglobal(event->globalpos()).y()); qwidget::mousepressevent(event); } void mousereleaseevent(qmouseevent *event) { qdebug()<<"gridwell mouse released @ x="<<event->globalpos().x()<<" y ="<<event->globalpos().y(); qwidget::mousereleaseevent(event); endpos = new qpoint; endpos->setx(qwidget::mapfromglobal(event->globalpos()).x()); endpos->sety(qwidget::mapfromglobal(event->globalpos()).y()); lineitemptr = c1->addline(startpos->x(),startpos->y(),(endpos->x())- (startpos->x()),(endpos->y())-(startpos->y()) ); lineitemptr->setvisible(true); cv2->update(); } void mousemoveevent ( qmouseevent * event ) { //show x , y coordinate values of mouse cursor here qdebug()<<"gridwell x:"<<qstring::number(event->x())<<"-- y:"<<qstring::number(event->y()); endpos = new qpoint; }
finally, here keyeater
cut , pasted documentation:
class keypresseater : public qobject { q_object public: keypresseater(qobject *parent = q_nullptr) : qobject (parent){ } protected: bool eventfilter(qobject *obj, qevent *event) { return(false); // propagates events } };
any ideas why cv1
not scene c1
( since not print "hello world". cv2
does?
thanks help!
update: did more investigation , changed following routine:
void mygraphicsview::mousereleaseevent(qmouseevent *event) { qdebug()<<" mouse relase pressed @ x="<<event->x()<<" y ="<<event->y(); endpos = new qpoint; endpos->setx(event->x()); endpos->sety(event->y()); c->addline(0,0,event->x(),50); }
now, when click inside cv1 view area, see line in cv2 since both cv2 , cv1 views pointing same scene (cv1->c = c1; line in great.cpp). proves scene not mapped correctly in cv2 since not "hello world" shows up. baffled since seems simple object being passed constructor shown in many examples.
Comments
Post a Comment