Results 1 to 3 of 3

Thread: best way to draw cursor crosshairs lines to a GLWidget and viewport mouse coordinates

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    287
    Thanks
    23
    Thanked 31 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default best way to draw cursor crosshairs lines to a GLWidget and viewport mouse coordinates

    Hi

    I'm starting a small cad editor just for fun using QGLWidgt.

    1)To display a crosshair cursor lines I am updating the mouse move event and calling a function DrawCursorCrossHairs() from paintGL(). This works nice but doenst seem the right approach has in time, and having a lot of entities (lines, object drawing, …) they will be constantly updated with the mouse movement. Is there any other better way do display the cursor cross hair lines, without calling the paintGL() ?

    Code:

    Qt Code:
    1. void DrawGLWidget::paintGL()
    2. {
    3. glPushMatrix();
    4. glTranslated(tx,ty,tz);
    5. glPushMatrix();
    6. glScaled(scale,scale,scale);
    7.  
    8.  
    9. Draw();
    10. DrawCursorCrossHairs();
    11.  
    12.  
    13. glPopMatrix();
    14. glPopMatrix();
    15. }
    16.  
    17. …............
    18.  
    19. void DrawGLWidget::DrawCursorCrossHairs()
    20. {
    21.  
    22. glPushMatrix();
    23.  
    24. glColor3d(1.0,0.0,0.0);
    25. glBegin(GL_LINE_STRIP);
    26. glVertex2d( m_CursorCrossHairs.x, height());
    27. glVertex2d( m_CursorCrossHairs.x, -height());
    28. glEnd();
    29.  
    30. glBegin(GL_LINE_STRIP);
    31. glVertex2d( -width(), m_CursorCrossHairs.y);
    32. glVertex2d( width(), m_CursorCrossHairs.y);
    33. glEnd();
    34.  
    35. glPopMatrix();
    36.  
    37. }
    38.  
    39.  
    40. void DrawGLWidget::mouseMoveEvent(QMouseEvent *event)
    41. {
    42. int ww=width();
    43. int hh=height();
    44. if (ww <= hh)
    45. {
    46.  
    47. m_CursorCrossHairs.y = -2*Range/ww*event->pos().y()+Range*hh/ww;
    48. m_CursorCrossHairs.x = 2*Range/ww*event->pos().x()-Range;
    49. }
    50. else
    51. {
    52. m_CursorCrossHairs.y = -2*Range/hh*event->pos().y()+Range;
    53. m_CursorCrossHairs.x = 2*Range/hh*event->pos().x()-Range*ww/hh;
    54. }
    55.  
    56. // m_CursorCrossHairs.x = event->pos().x();
    57. // m_CursorCrossHairs.y = event->pos().y();
    58.  
    59. updateGL();
    To copy to clipboard, switch view to plain text mode 
    }


    2)

    I defined the viewport like this:

    Qt Code:
    1. void DrawGLWidget::resizeGL(int width, int height)
    2. {
    3. int w=width;
    4. int h=height;
    5. // Prevent a divide by zero, when window is too short
    6. // (you cant make a window of zero width)
    7. if(h == 0)//altura
    8. h = 1;
    9.  
    10. // Set the viewport to be the entire window
    11. glViewport(0, 0, w, h);
    12. //Reset projection matrix stack
    13. glMatrixMode(GL_PROJECTION);
    14. // Reset the coordinate system before modifying
    15. glLoadIdentity();
    16.  
    17. if (w <= h)
    18. glOrtho(-(Range),Range,-Range*h/w,Range*h/w,-(Range*2),Range*2);
    19. else
    20. glOrtho(-(Range*w/h),Range*w/h,-Range,Range,-(Range*2),Range*2);
    21.  
    22. glMatrixMode(GL_MODELVIEW);
    23. glLoadIdentity();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Has you see I made some math effort in the mouseMoveEvent() to translate the mouse coordinates to the GLWidget coordinates. Is there some direct / easy way to translate mouse / viewport coordinates ?

    I have attach what I got so far.
    Attached Files Attached Files
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

Similar Threads

  1. Viewport and scene coordinates
    By aurelius in forum Newbie
    Replies: 2
    Last Post: 30th October 2008, 23:47
  2. How to draw some points and lines?
    By luffy27 in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2006, 16:47
  3. How to draw lines with the right width?
    By albanelporto in forum Qt Programming
    Replies: 3
    Last Post: 22nd November 2006, 11:51
  4. QScrollArea relative viewport coordinates
    By kalanikta in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2006, 10:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.