MouseMove event only work if mouse button is pressed.
-
I'm trying to make some code that get mouse coordinates when mouse is moved inside MainWindow, the code below only work if some mouse button is pressed, how can i make it work even if no mouse button is pressed? I tried "setMouseTracking(true)" but it not work!
@void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
int x = event->x();
int y = event->y();
}@ -
Use the static method "QCursor::pos()":http://qt-project.org/doc/qt-4.8/qcursor.html#pos to get the position of the cursor in global screen coordinates.
-
you must setMouseTracking(true);
-
setMouseTracking(true) doesn't work!
-
Now 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);
}
@How can i display the coordinates in the label?
-
@
m_scene = new QGraphicsScene(this);
setScene(m_scene);
setMouseTracking(true);
@Try these in the constructor.
-
@TFabbri error: C2065: 'm_scene': undeclared identifier. C3861: 'setScene': identifier not found.
How do I solve it?@rohit1729 This thread is 7 years old already!
The error tells you that m_scene is not declared. So, did you declare it? -
Hi
its just a member in .h
QGraphicsScene* m_scene; -
@rohit1729 This thread is 7 years old already!
The error tells you that m_scene is not declared. So, did you declare it? -
@rohit1729 Well, you did not declare setScene either...
-
@rohit1729
hi
that is because it belongs to
https://doc.qt.io/qt-5/qgraphicsview.html#setScene
and unless you in a qgraphicsview subclass, it wont have that method.You can just create a new view, if you dont already uses one.