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);
}
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
uiof any widget designed using auifile (QtDesigner) isprivate: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:
The implementation might look like this:
And you should then call it like this:
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):
And put this into your implementation: