Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3761546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:38:33+00:00 2026-05-19T10:38:33+00:00

The easy way to create the long cross line cursor (as long as viewport)

  • 0

The easy way to create the long cross line cursor (as long as viewport) is create a cross line graphicsItem, when mouse moved, set the item’s pos property.
But this way will be very slow when the scene is complex, because it should update the whole viewport to update the cursor’s pos.

The another easy way is setCursor(QCursor(..)),use a QPixmap to define the long cross line, this way will very fast , but the cursor will exceed the viewport rect.

Is there another way to show a long cross line cursor fastly?

Thanks very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:38:34+00:00Added an answer on May 19, 2026 at 10:38 am

    If I understand correctly, you want to draw an horizontal line and a vertical line, crossing at the cursor position, and being as large as the viewport is.

    A poosible solution would be to reimplement QGraphicsScene::drawForeground() to draw the two lines with the painter.

    The problem is that the scene doesn’t know about the mouse position. This means the view will have to track it and inform the scene when the mouse position has changed.

    To do that, you’ll have to create your own GraphicsScene (inheriting QGraphicsScene) and your own GraphicsView (inheriting QGraphicsView).

    On your GraphicsView constructor, you’ll have to start mouse tracking. This will make your you receive a mouseMoveEvent each time the mouse moves inside the view :

    GraphicsViewTrack::GraphicsViewTrack(QWidget* parent) : QGraphicsView(parent) {
        setMouseTracking(true);
    }
    
    void GraphicsViewTrack::mouseMoveEvent(QMouseEvent* pEvent) {
        QPointF MousePos = this->mapToScene(pEvent->pos());
        emit mousePosChanged(MousePos.toPoint());
    }
    

    As you can see in the code snippet above, the view is emitting a signal (mousePosChanged) to which the scene will be connected. This signal contains the mouse position, converted to the scene’s coordinates.

    Now, on the scene side, you have to add a slot which will be called when the mouse position changed, store the new mouse position in a member variable and reimplement QGraphicsScene::drawForeground() :

    void GraphicsSceneCross::drawForeground(QPainter* painter, const QRectF& rect) {
        QRectF SceneRect = this->sceneRect();
    
        painter->setPen(QPen(Qt::black, 1));
        painter->drawLine(SceneRect.left(), m_MousePos.y(), SceneRect.right(), m_MousePos.y());
        painter->drawLine(m_MousePos.x(), SceneRect.top(), m_MousePos.x(), SceneRect.bottom());
    }
    
    void GraphicsSceneCross::onMouseChanged(QPoint NewMousePos) {
        m_MousePos = NewMousePos; // Store the mouse position in a member variable
        invalidate(); // Tells the scene it should be redrawn
    }
    

    The last thing to do is connect the GraphicsView’s signal to the GraphicsScene slot.

    I’ll let you check if this solution is acceptable performance wise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's an easy way to create a directory on an FTP server using C#?
Is there an easy way to create a multiline string literal in C#? Here's
Is there an easy way to create a list of only the time zones
Is there a easy way to create documentation pages based on xml documentation in
Is there an easy way to add line number information to created nodes in
Is there any easy way to create a class that uses IFormatProvider that writes
Is there an easy way to log PHP errors to a log for a
Is there an easy way to generate docs for REST api direct from a
Is there an easy way of getting the datetime of the last change to
Is there an easy way in javascript to only escape certain ranges of control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.