Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. mousePressEvent working for one window but not another?
Qt 6.11 is out! See what's new in the release blog

mousePressEvent working for one window but not another?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • adalovegirlsA Offline
    adalovegirlsA Offline
    adalovegirls
    wrote on last edited by adalovegirls
    #1

    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#L136

    And here is where it is working:
    https://github.com/AdaLovegirls/Chip-8-Qt/blob/1e73d3c108082bba53f4f7d2f5b8174f61a74d30/mainwindow.cpp#L109

    No 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!

    jsulmJ 1 Reply Last reply
    0
    • adalovegirlsA adalovegirls

      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#L136

      And here is where it is working:
      https://github.com/AdaLovegirls/Chip-8-Qt/blob/1e73d3c108082bba53f4f7d2f5b8174f61a74d30/mainwindow.cpp#L109

      No 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!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      adalovegirlsA 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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?

        adalovegirlsA Offline
        adalovegirlsA Offline
        adalovegirls
        wrote on last edited by adalovegirls
        #3

        @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..

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          adalovegirlsA 1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            adalovegirlsA Offline
            adalovegirlsA Offline
            adalovegirls
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              adalovegirlsA 1 Reply Last reply
              0
              • SGaistS SGaist

                @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.

                adalovegirlsA Offline
                adalovegirlsA Offline
                adalovegirls
                wrote on last edited by
                #7

                @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!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  adalovegirlsA 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    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.

                    adalovegirlsA Offline
                    adalovegirlsA Offline
                    adalovegirls
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      adalovegirlsA 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        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.

                        adalovegirlsA Offline
                        adalovegirlsA Offline
                        adalovegirls
                        wrote on last edited by
                        #11

                        @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?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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 ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          adalovegirlsA 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            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 ?

                            adalovegirlsA Offline
                            adalovegirlsA Offline
                            adalovegirls
                            wrote on last edited by
                            #13

                            @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. :)

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved