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. How disable QMouseMoveEvent of QPushButton in user-defined titlebar ?
Forum Updated to NodeBB v4.3 + New Features

How disable QMouseMoveEvent of QPushButton in user-defined titlebar ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.4k Views
  • 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
    sonichy
    wrote on 21 Feb 2018, 03:07 last edited by
    #1

    When i design a titlebar myself, the QPushButton of Minimize,Maxmize,Close in the titlebar will also trigger QMouseMoveEvent which I don't want.

    void TitleBar::mousePressEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::LeftButton){    
            relativePos = event->pos();
        }
    }
    
    void TitleBar::mouseMoveEvent(QMouseEvent *event)
    {    
        emit moveMainWindow(event->globalPos() - relativePos);
    }
    

    Source code: https://github.com/sonichy/NeteaseMusic

    替代文字

    https://github.com/sonichy

    D 1 Reply Last reply 21 Feb 2018, 04:42
    0
    • S sonichy
      21 Feb 2018, 03:07

      When i design a titlebar myself, the QPushButton of Minimize,Maxmize,Close in the titlebar will also trigger QMouseMoveEvent which I don't want.

      void TitleBar::mousePressEvent(QMouseEvent *event)
      {
          if(event->button() == Qt::LeftButton){    
              relativePos = event->pos();
          }
      }
      
      void TitleBar::mouseMoveEvent(QMouseEvent *event)
      {    
          emit moveMainWindow(event->globalPos() - relativePos);
      }
      

      Source code: https://github.com/sonichy/NeteaseMusic

      替代文字

      D Offline
      D Offline
      Devopia53
      wrote on 21 Feb 2018, 04:42 last edited by
      #2

      @sonichy

      You can simply set the flag to handle it. like this:

      // in titlebar.h
          bool isClicked{false};
          void mouseReleaseEvent(QMouseEvent *event);
      
      // in titlebar.cpp
      void TitleBar::mousePressEvent(QMouseEvent *event)
      {
          if(event->button() == Qt::LeftButton){
              //qDebug() << "mousePress" << event->pos();
              relativePos = event->pos();
              isClicked = true;
          }
      }
      
      void TitleBar::mouseMoveEvent(QMouseEvent *event)
      {
          //qDebug() << event->globalPos() << "-" << relativePos << "=" << event->globalPos() - relativePos;
          if (isClicked)
              emit moveMainWindow(event->globalPos() - relativePos);
      }
      
      void TitleBar::mouseReleaseEvent(QMouseEvent *event)
      {
          Q_UNUSED(event)
          isClicked = false;
      }
      
      
      1 Reply Last reply
      2
      • A Offline
        A Offline
        Andeol
        wrote on 23 Feb 2018, 13:50 last edited by Andeol
        #3

        You could use an eventFilter (http://doc.qt.io/archives/qt-4.8/qobject.html#eventFilter).

        Most of the time, just using mousePressEvent is better if it's enough for what you want.
        But if you need to be able to check which item is receiving an event, you can check it by testing the QObject * argument in the eventFilter method.

        Developer for R++ : https://rplusplus.com/

        S 1 Reply Last reply 24 Feb 2018, 02:45
        2
        • A Andeol
          23 Feb 2018, 13:50

          You could use an eventFilter (http://doc.qt.io/archives/qt-4.8/qobject.html#eventFilter).

          Most of the time, just using mousePressEvent is better if it's enough for what you want.
          But if you need to be able to check which item is receiving an event, you can check it by testing the QObject * argument in the eventFilter method.

          S Offline
          S Offline
          sonichy
          wrote on 24 Feb 2018, 02:45 last edited by
          #4

          @Andeol Under your advice, I solved the problem!

          TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
          {
              ...
              pushButton_minimize->installEventFilter(this);
              pushButton_maximize->installEventFilter(this);
              pushButton_close->installEventFilter(this);
              ...
          }
          
          bool TitleBar::eventFilter(QObject *obj, QEvent *event)
          {
              if ( (obj == pushButton_minimize || obj == pushButton_maximize || obj == pushButton_close) && event->type() == QEvent::MouseMove ) {
                  return true;    // filter
              } else {
                  return false;
              }
              // pass the event on to the parent class
              return QWidget::eventFilter(obj, event);
          }
          

          https://github.com/sonichy

          1 Reply Last reply
          0

          4/4

          24 Feb 2018, 02:45

          • Login

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