c++ - An issue when rendering a line depending on Mouse position -
i have opengl widget, want render line dependent of mouse positions, follows:
glpushmatrix(); glenable(gl_blend); glblendfunc(gl_src_alpha, gl_one_minus_src_alpha); glcolor4f(1, 0, 0, 0.1); glscalef(a, b, 0); glbegin(gl_lines); glvertex2f(pushedx, pushedy); glvertex2f(currentx, currenty); glend(); glflush(); gldisable(gl_blend); glpopmatrix();
where:
pushedx=buttonpresscoordinates.x(); pushedy=buttonpresscoordinates.y(); currentx=mousecurrentposition.x(); currenty=mousecurrentposition.y();
the rendering goes good, , line rendered required when move mouse connecting line pushed , current coordinates.
but:
the issue is, when press mouse somewhere on widget , don't move it, generates randomly (as think) (x,y) , connects line coordinates of mouse pressed position. though when start moving mouse starts working fine.
please, fix bug.
edit
the code of assigning current values of mouse
void mainwindow::mousemoveevent(qmouseevent *eventmove) { if(eventmove->buttons() & qt::leftbutton) { glwidget *widget = this->findchild<glwidget *>("glwidget"); float x = widget->getnormalizedwidth(widget->mapfromglobal(qcursor::pos()).x()); float y = widget->getnormalizedheight(widget->mapfromglobal(qcursor::pos()).y()); float y_ = 1.0 - y; mousecurrentposition.setx(x); mousecurrentposition.sety(y_); widget->setcurrentx(mousecurrentposition.x()); widget->setcurrenty(mousecurrentposition.y()); } }
note: qpointf mousecurrentposition;
, getnormalizedwidth(...)
defined fundction works perfect.
edit-2
the mouse click coordinates updated follows:
setmousetracking(true); m = true; glwidget *widget = this->findchild<glwidget *>("glwidget"); float x = widget->getnormalizedwidth(widget->mapfromglobal(qcursor::pos()).x()); float y = widget->getnormalizedheight(widget->mapfromglobal(qcursor::pos()).y()); float y_ = 1.0 - y; buttonpresscoordinates.setx(x); buttonpresscoordinates.sety(y_); qdebug() << buttonpresscoordinates.x() << buttonpresscoordinates.y(); widget->setq(true); widget->setpushedx(buttonpresscoordinates.x()); widget->setpushedy(buttonpresscoordinates.y());
one thing can try update currentx
, currenty
in mousemoveevent
when leftbutton
not pressed:
void mainwindow::mousemoveevent(qmouseevent *eventmove) { glwidget *widget = this->findchild<glwidget *>("glwidget"); float x = widget->getnormalizedwidth(widget->mapfromglobal(qcursor::pos()).x()); float y = widget->getnormalizedheight(widget->mapfromglobal(qcursor::pos()).y()); float y_ = 1.0 - y; if(eventmove->buttons() & qt::leftbutton) { buttonpresscoordinates.setx(x); buttonpresscoordinates.sety(y_); widget->setpushedx(buttonpresscoordinates.x()); widget->setpushedy(buttonpresscoordinates.y()); } else { mousecurrentposition.setx(x); mousecurrentposition.sety(y_); widget->setcurrentx(mousecurrentposition.x()); widget->setcurrenty(mousecurrentposition.y()); } }
Comments
Post a Comment