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. detect if mouse buttons are pressed when sliding into widget
Forum Updated to NodeBB v4.3 + New Features

detect if mouse buttons are pressed when sliding into widget

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 5.0k Views 3 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #7

    i tryed it out however i still have the same issue:

    
    bool CellInputHandler::eventFilter(QObject *watched, QEvent *event)
    {
        auto cell = qobject_cast<Cell *>(watched);
    
        if(event->type() == QEvent::MouseButtonPress){
           auto mouse_event = static_cast<QMouseEvent*>(event);
           cell->handleMousePressEvent(mouse_event);
           return true;
        }
        if(event->type() == QEvent::MouseButtonRelease){
           auto mouse_event = static_cast<QMouseEvent*>(event);
           cell->handleMouseReleaseEvent(mouse_event);
           return true;
        }
        if(event->type() == QEvent::MouseMove) {
            auto mouse_event = static_cast<QMouseEvent*>(event);
            QRect rect{cell->mapToGlobal(QPoint(0, 0)), cell->size()};
    
            if(rect.contains(mouse_event->globalPos())) {
    
                if(mouse_event->buttons() & Qt::LeftButton &&
                    mouse_event->buttons() & Qt::RightButton) {
    
                    cell->handleMouseMoveEventInside(mouse_event);
                }
            }
            else {
                cell->handleMouseMoveEventOutside(mouse_event);
            }
            return true;
        }
        return false;
    }
    
    
    

    Press and Release seem to work fine like they already did. However the move still only detects one widget.

    In Cell i added this:

    void Cell::handleMouseMoveEventInside(QMouseEvent *event)
    {
            qDebug() << "Entered";
    }
    
    void Cell::handleMouseMoveEventOutside(QMouseEvent *event)
    {
            qDebug() << "Left";
    }
    

    Still i only see Entered and Left from the widget i first clicked down the mouse buttons

    1 Reply Last reply
    0
    • S sandro4912

      I want to do the following:

      I have two exactly the same widgets which are next to each other. I want to push down a mouse button in the first one and then slide into the second widget to perform a action there. So the second widget should detect that the mouse button is down and i slided in.

      However it looks like the second widget never gets informed that i slided into it. Strangely I can see the slide in if i go out of the first widget and back in.

      void Cell::mouseMoveEvent(QMouseEvent *event)
      {
          if(!mouseIsOutside(event)  &&
                  event->buttons() & Qt::LeftButton ) {
      
              qDebug() << "Entered";
      
          }
      }
      
      bool Cell::mouseIsOutside(QMouseEvent *event)
      {
          auto pos = event->pos();
          auto rect = this->rect();
      
          if(pos.x() < rect.x()) {
              return true;
          }
          if(pos.x() > rect.x() + rect.width()) {
              return true;
          }
          if(pos.y() < rect.y()) {
              return true;
          }
          if(pos.y() > rect.y() + rect.height()) {
              return true;
          }
          return false;
      }
      
      A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #8

      @sandro4912 It sounds a bit like a drag-and-drop operation to me. Wouldn't that work for you?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #9

        You mean using drag and drop in the event filter to transfer that mouse buttons are pressed both?

        mrjjM 1 Reply Last reply
        0
        • S sandro4912

          You mean using drag and drop in the event filter to transfer that mouse buttons are pressed both?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @sandro4912
          Hi
          Do you have
          setMouseTracking(true);
          on widget 2 ?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by sandro4912
            #11

            @mrjj said in detect if mouse buttons are pressed when sliding into widget:

            Hi

            Yes it is enabled on both widgets(they are the same widgets)

            mrjjM 1 Reply Last reply
            0
            • S sandro4912

              @mrjj said in detect if mouse buttons are pressed when sliding into widget:

              Hi

              Yes it is enabled on both widgets(they are the same widgets)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @sandro4912
              Ok. :)
              was just a shot.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sandro4912
                wrote on last edited by
                #13

                I really wonder how the issue could get solved. Annother Information i maybe forgot to tell.

                Both widgets are inside annother widget. Could that help?

                mrjjM 1 Reply Last reply
                0
                • S sandro4912

                  I really wonder how the issue could get solved. Annother Information i maybe forgot to tell.

                  Both widgets are inside annother widget. Could that help?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  @sandro4912
                  Hi
                  The issue is that you only see

                  event->type() == QEvent::MouseMove

                  for one of the widgets ?

                  O 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @sandro4912
                    Hi
                    The issue is that you only see

                    event->type() == QEvent::MouseMove

                    for one of the widgets ?

                    O Offline
                    O Offline
                    ofmrew
                    wrote on last edited by
                    #15

                    @mrjj I believe that you need to set a variable, addressable from each cell, that indicates that the mouse button has been pressed. My concern with this is that what happens if the mouse button is released in another widget? Hover events might be helpful.

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      The problem is you're checking the rectangle of the wrong widget. As I said the widget grabs mouse when you press, so cell is always gonna be the widget that gets the events, the one you pressed on, not the one you're hovering over.

                      You need to get the rectangle of the other widget from somewhere.

                      Here's a little working example. Maybe you'll find it useful:

                      #include <QApplication>
                      #include <QPushButton>
                      #include <QHBoxLayout>
                      #include <QEvent>
                      #include <QDebug>
                      
                      struct FilterClass : public QObject
                      {
                          bool eventFilter(QObject*, QEvent* evt)
                          {
                              if (evt->type() == QEvent::MouseMove)
                              {
                                  auto move_evt = static_cast<QMouseEvent*>(evt);
                                  if (move_evt->buttons().testFlag(Qt::LeftButton) &&
                                      move_evt->buttons().testFlag(Qt::RightButton))
                                      qDebug() << "Mouse over" << qApp->widgetAt(move_evt->globalPos());
                              }
                      
                              return false;
                          }
                      };
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                      
                          FilterClass filter;
                          QWidget w;
                          w.setLayout(new QHBoxLayout());
                          w.layout()->addWidget(new QPushButton("Button 1"));
                          w.layout()->addWidget(new QPushButton("Button 2"));
                          for (auto c : w.children()) c->installEventFilter(&filter);
                      
                          w.show();
                          return a.exec();
                      }
                      
                      1 Reply Last reply
                      2
                      • S Offline
                        S Offline
                        sandro4912
                        wrote on last edited by
                        #17

                        I could solve the issue with the example code.

                        I had to add a member which remembers the last cell in the event filer. Than i could solve my Issue like this:

                        if(event->type() == QEvent::MouseMove) {
                            auto mouse_event = static_cast<QMouseEvent*>(event);
                            QRect rect{cell->mapToGlobal(QPoint(0, 0)), cell->size()};
                        
                            if(mouse_event->buttons().testFlag(Qt::LeftButton) &&
                                mouse_event->buttons().testFlag(Qt::RightButton)) {
                        
                                auto widget = qApp->widgetAt(mouse_event->globalPos());
                        
                                if(widget) {
                                    auto currentCell = qobject_cast<Cell *>(widget);
                        
                                    if(!currentCell) {
                                        if(mLastCell) {
                                            mLastCell->handleMouseMoveEventOutside(mouse_event);
                                        }
                                        mLastCell = nullptr;
                                        return true;
                                    }
                        
                                    if(currentCell != mLastCell) {
                                        if(mLastCell) {
                                            mLastCell->handleMouseMoveEventOutside(mouse_event);
                                        }
                                        currentCell->handleMouseMoveEventInside(mouse_event);
                                        mLastCell = currentCell;
                                    }
                                }
                                return true;
                            }
                        }
                        
                        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