Solved mousePressEvent working for one window but not another?
-
I have an application with two separate MainWindows. I'm trying to have each perform an action on being clicked/receiving focus, but it's only working for one window and I can't figure out what I'm doing wrong.
I'm not sure the exact info that one may need to help me fix this, so I will link the GitHub repo the code is on.
Here is the code for the window that is not working:
https://github.com/AdaLovegirls/Chip-8-Qt/blob/1e73d3c108082bba53f4f7d2f5b8174f61a74d30/debugger.cpp#L136And here is where it is working:
https://github.com/AdaLovegirls/Chip-8-Qt/blob/1e73d3c108082bba53f4f7d2f5b8174f61a74d30/mainwindow.cpp#L109No matter where I click in the window, mousePressEvent() is never called. The other two events, showEvent() and closeEvent(), work correctly.
The README has screenshots as well if that might be helpful. Thank you so much to anyone that can help!
-
@adalovegirls said in mousePressEvent working for one window but not another?:
mousePressEvent() is never called
How did you verify this? Maybe sw->running is false?
-
@jsulm I double checked just now. I removed the if statement in mousePressEvent in debugger.cpp and set a breakpoint on sw->breakPoint(), and set a breakpoint in mousePressEvent on the if statement in mainwindow.cpp: the breakpoint in debugger.cpp is never hit, the breakpoint in mainwindow.cpp is hit as expected.
void Debugger::mousePressEvent(QMouseEvent *event) { -> sw->breakPoint(); }
void MainWindow::mousePressEvent(QMouseEvent *event) { -> if (!sw->running) sw->run(); }
Could it have something to do with the combination of frames and widgets I'm using in debugger.ui? That's the only difference between the two windows I can think of..
-
Hi,
Out of curiosity, why not call the base class implementation of the method ?
Also, why are both QMainWindow based ?Does any of your widget have mouse tracking enabled ?
-
@SGaist To answer the first two, well, because I'm still learning and there's many things I don't know. How could I call the base class implementation and convert it to a QWidget?
No, it wasn't enabled. I enabled it and no change.
-
@adalovegirls said in mousePressEvent working for one window but not another?:
How could I call the base class implementation and convert it to a QWidget?
You do that in you showEvent implementation :-)
To change your base class, change the class you derive your widget from.
-
@SGaist Actually, I realized I don't understand what you mean by "base class implementation". I changed the Debugger window to a QWidget, but am still having the problem where mousePressEvent doesn't register. :( Thank you for the help so far though!
-
In your showEvent method, you call QMainWindow::showEvent. You are thus calling the base class implementation of that function.
What I would do (since you are learning) is start with just a skeleton project with two simple widgets and experiment there.
Next you can start adding stuff.
-
@SGaist Ah. I had copied that from something online. I fixed that. And I'll do that.
I still could use help with the mousePressEvent thing though.
-
Hence my suggestion of going step by step. I do not see anything obviously wrong with the classes you are using.
Note that I did not clone the full project.
-
@SGaist So. I did what you said, making a new window and playing around, and I discovered that mousePressEvent does not work when clicking inside a ListWidget. I suppose this makes sense, but is there a way around this?
Something else that would work, is calling the function to pause the emulator when the Debugger window receives focus. I tried to figure out how to do that, with QApplication::focusChanged() or with an event filter, but I couldn't figure out how to get either of those to work..
Do you or anyone else know how I can do this?
-
Ah ! I think I understand now your issue. You were expecting your "main widget" mousePressEvent to be called when you were clicking inside a widget contained by that "main widget", is that correct ?
If so, then yes, I think that the event filter would be simpler. What did you try with it ?
-
@SGaist That is correct. I was wondering if that might be the case, but I wasn't sure.
In the course of writing this reply, I Googled "qt on window focus" again and found this thread that I hadn't seen before, and that works for my case! Thank you very much for your continued help, I probably wouldn't have come across the solution without it. :)