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. MouseReleaseEvent only working for right button?
Forum Updated to NodeBB v4.3 + New Features

MouseReleaseEvent only working for right button?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.3k Views 1 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.
  • G Offline
    G Offline
    godplusplus
    wrote on 19 Mar 2012, 06:28 last edited by
    #1

    Hello everyone,

    I created a splitter, and I need to call a function after the person puts the splitter in a new position (not when the splitter moves, but when the splitter moves and then the user lets go of the mouse button).

    I set a flag on the splitterMoved slot and then I tried doing the following:

    In the .h
    [code]
    protected:
    void mouseReleaseEvent ( QMouseEvent *event );
    [/code]

    In the .cpp

    [code]
    void MainWindow::mouseReleaseEvent( QMouseEvent *event )
    {
    if(m_WasSplitterMoved)
    {
    //Magic happens here
    }
    m_WasSplitterMoved = false;
    }
    [/code]

    But it's not working! I put a breakpoint there and realized that it only gets called when the RIGHT mouse button is released, but that doesn't help me since to move the splitter, you use the left mouse button.

    Can someone tell me what I'm doing wrong? Or another way to catch when the player has moved the splitter to a new position and let go of the mouse?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on 19 Mar 2012, 06:56 last edited by
      #2

      You receive neither the left mouse button nor the right mouse button this way, because the mouse events are handeled by the splitter handle widget, not the main window widget. Thus you want to receive events for the splitter handle, not the main window.

      The easiest solution to this is installing an event filter on the splitter handle.
      @
      class MainWindow : public QMainWindow
      {
      ...
      public:
      MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0) : QMainWindow(parent, flags)
      {
      m_Splitter = new QSplitter;
      m_Splitter->addWidget(...);
      ...
      m_Splitter->handle(1)->installEventFilter(this);
      }

      protected:
      bool eventFilter(QObject *object, QEvent *event)
      {
      if((object == m_Splitter->handle(1)) && (event->type() == QEvent::MouseButtonRelease))
      {
      if (m_WasSplitterMoved == true)
      {
      // magic happens here
      }
      }

          return QMainWindow::eventFilter(object, event);
      }
      

      }
      @
      Brain to terminal. Not tested. Exemplary.

      Another annotation: if you override event handlers, always make sure you call the base class implementation (unless you explicitly don't want to do so).
      @
      void MainWindow::mouseReleaseEvent( QMouseEvent *event )
      {
      ...
      QMainWindow::mouseReleaseEvent(event);
      }
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        godplusplus
        wrote on 19 Mar 2012, 07:14 last edited by
        #3

        Thanks! That worked!
        I had actually tried to use the event filter earlier, but it hadn't worked since I didn't know about installing it on the handle instead of in the splitter itself.

        1 Reply Last reply
        0

        1/3

        19 Mar 2012, 06:28

        • Login

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