Results 1 to 3 of 3

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

  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

  2. #2
    Join Date
    Jun 2021
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Cool Re: best way to draw cursor crosshairs lines to a GLWidget and viewport mouse coordin

    hi. i did something like that about 20-30 years ago, except it was in Commodore BASIC 2.0 on my trusty old C64 (i kid you not -- i was newly exposed to AutoCAD on IBM PCs and i wanted to do something similar)
    (and no i no longer have my old source code but i sure wish i did -- and mine drew crosshairs using ONLY ASCII characters driven by cursor keys or maybe a joystick, and it was super slow, too! lol)

    so i was wondering if you ever figured your problem out and found out how to do what you asked? yes i know this post is 10 years old lol
    i want to do something like this and learn QT5 as well. I have learned some C++ now as it is (i am a newb).

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,970
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: best way to draw cursor crosshairs lines to a GLWidget and viewport mouse coordin

    Commodore BASIC 2.0 on my trusty old C64
    Ah, the good old days. Dinosaurs. Before the wheel, fire, and all of those other things that have complicated our lives. Back when pixels came in only one color.

    OpenGL has changed a lot in 10 years, too. In particular, very little is done PC-side to draw. All of the graphics are created and loaded into OpenGL data structures and then moved into the GPU and drawn with the help of shaders and other GPU-side functions. So the whole approach used by the OP is obsolete.

    If you are just getting started in Qt and C++, I would recommend beginning with Qt's own graphics APIs and getting comfortable with event-driven program architecture if you have never done anything using that model. Jumping into OpenGL with both feet would quickly put you in over your head.

    Your Qt distribution comes with a ton of examples covering basic graphics, the Graphics / View architecture, and Qt Quick / QML for declarative UI programming.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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.