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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on 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
    0
    • sonichyS sonichy

      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 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
      • AndeolA Offline
        AndeolA Offline
        Andeol
        wrote on 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/

        sonichyS 1 Reply Last reply
        2
        • AndeolA Andeol

          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.

          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on 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

          • Login

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