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 8651771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:09:05+00:00 2026-06-12T14:09:05+00:00

I’m using mouse hook to get the mouse coordinates, but i’m trying to display

  • 0

I’m using mouse hook to get the mouse coordinates, but i’m trying to display it on my label and the code don’t work.

LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode==HC_ACTION)
    {
    POINT p;
    GetCursorPos(&p);
    MainWindow* mw = new MainWindow();
    mw->ui->label->setText(QString::number(p.x)+"|"+QString::number(p.y));//this code don't work!
    }
    return CallNextHookEx(NULL,nCode,wParam,lParam);
 }
  • 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-06-12T14:09:07+00:00Added an answer on June 12, 2026 at 2:09 pm

    You didn’t provide any information about the error, not even if your code compiles. So the following are only assumptions. Please provide better descriptions in your next question.

    You made two mistakes. First, you create a new main window every time your method gets called. I don’t think this is what you want, but rather one single main window instance which gets updated in every call. So you need a globally accessible pointer to an existing main window instance.

    The second thing is that your code won’t compile because the member ui of any widget designed using a ui file (QtDesigner) is private:

    class MainWindow : public QMainWindow
    {
       ...
    private:
       Ui::MainWindow *ui;
    };
    

    So you may not access it from outside this class. This has a very good reason: How the widget (main window) is designed should not bother code outside this class (principle of information / implementation hiding).

    You should provide a public method to set the data:

    class MainWindow : public QMainWindow
    {
    public:
        void updateCursorPos(int x, int y);
    };
    

    The implementation might look like this:

    MainWindow::updateCursorPos(int x, int y)
    {
        ui->label->setText(QString::number(x) + "|" + QString::number(y));
    }
    

    And you should then call it like this:

    POINT p;
    GetCursorPos(&p);
    mw->updateCursorPos(p.x, p.y);
    

    Finally, I can’t see the reason why you have to use native code to get the cursor coordinates since Qt has a very simple method for this: QPoint QCursor::pos()

    You can query the cursor positions periodically using a timer, for example. For this, add a slot with the following signature (you don’t need the method from above anymore):

    class MainWindow : public QMainWindow
    {
    public slots:
        void updateCursorPos();
    };
    

    And put this into your implementation:

    // constructor
    MainWindow::MainWindow(...) : ...
    {
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(updateCursorPos()));
        timer->setInterval(50);
        timer->start();
    }
    
    void MainWindow::updateCursorPos()
    {
        QPoint p = QCursor::pos();
        ui->label->setText(QString::number(p.x) + "|" + QString::number(p.y));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the mouse cords after using a WndProc hook to
I am trying to create a mouse hook using the Windows API. Currently, I
Basically what I'm trying to do is have a picture rotate using mouse events.
How do I get a tooltip that is attached to the mouse cursor using
I am trying to draw a circle using mouse on the canvas using mouse
I am using the JS API to display my map. Here is the code:
I downloaded a mouse hook sample, which didn't work. So I stripped all the
I have this code in which a user draws some polygon using mouse pointer
I've set up activate-mark-hook and deactivate-mark-hook, but they work only when selecting text by
In my c# project I’m trying to intercept mouse clicks from another program, but

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.