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 to use mouse events and signals together
Forum Updated to NodeBB v4.3 + New Features

How to use mouse events and signals together

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.7k 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I overridden the mouse event function in a custom class that inherited the list widget and implemented the necessary functions there.
    It was necessary to distinguish the list widget items, so the list widget signal itempressed was processed as a slot.

    It is expected that it is a problem with the mouse event, so I commented out the mouse event functions and tested it, and it works normally.

    Is there any way to use mouse event function and Itempressed signal together?

    jsulmJ 1 Reply Last reply
    0
    • I IknowQT

      @jsulm

      class usrCntrListWidget : public QListWidget
      {
      	Q_OBJECT
      
      private:
      	bool		m_bClicked;
      	bool		m_bVisibleCheckBox;
      	int			m_nCount;
      	QTimer*		m_pTimer;
      
      public:
      	usrCntrListWidget(QWidget *parent);
      	~usrCntrListWidget();
      	void	VisibleCheckBox(bool bShow);
      
      public slots:
      	void	GetLastTime();
      	void	VisibleCheckBox(QListWidgetItem* item);
      
      protected:
      	void    mousePressEvent(QMouseEvent* event) override;
      	void    mouseReleaseEvent(QMouseEvent* event) override;
      	void    mouseDoubleClickEvent(QMouseEvent* event) override;
      };
      
      
      ------------------------------- cpp -------------------------------
      void usrCntrListWidget::mousePressEvent(QMouseEvent* event)
      {
      	if (event->button() & Qt::LeftButton)
      	{
              qDebug() << "mousePressEvent";
              m_pTimer->start(1000);
          }
      }
      
      void usrCntrListWidget::mouseReleaseEvent(QMouseEvent* event)
      {
          if (event->button() & Qt::LeftButton)
          {
              qDebug() << "mouseReleaseEvent";
      
      		if (m_pTimer->isActive())
                  m_pTimer->stop();
          }
      }
      
      usrCntrListWidget::usrCntrListWidget(QWidget *parent)
      	:   m_bClicked(false),
              m_nCount(0),
              m_bVisibleCheckBox(false),
              m_pTimer(new QTimer(this))
      {
      	this->setMouseTracking(true);
      	this->viewport()->installEventFilter(this);
          this->connect(this->m_pTimer, SIGNAL(timeout()), this, SLOT(GetLastTime()));
          this->connect(this, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(VisibleCheckBox(QListWidgetItem*)));
      }
      
      void usrCntrListWidget::VisibleCheckBox(QListWidgetItem* item) 
      {
          qDebug() << "VisibleCheckBox";
      
      	usrCntrListItem* pItem = qobject_cast<usrCntrListItem*>(this->itemWidget(item));
          pItem->SetCheckVisible(!pItem->GetCheckStatus());
      }
      
      void usrCntrListWidget::mouseDoubleClickEvent(QMouseEvent* event)
      {
          if (event->button() & Qt::LeftButton)
          {
              qDebug() << "mouseDoubleClickEvent";
      
              if(m_bVisibleCheckBox)
                  VisibleCheckBox(false);
          }
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #4

      @IknowQT Yes, you're doing it wrongly.
      Take a look at https://doc.qt.io/qt-5/eventsandfilters.html "Event Handlers".
      " If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class's implementation."
      From that link:

      void MyCheckBox::mousePressEvent(QMouseEvent *event)
      {
          if (event->button() == Qt::LeftButton) {
              // handle left mouse button here
          } else {
              // pass on other buttons to base class
              QCheckBox::mousePressEvent(event); // HERE the base class implementation is called
          }
      }
      

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

      I 1 Reply Last reply
      2
      • I IknowQT

        I overridden the mouse event function in a custom class that inherited the list widget and implemented the necessary functions there.
        It was necessary to distinguish the list widget items, so the list widget signal itempressed was processed as a slot.

        It is expected that it is a problem with the mouse event, so I commented out the mouse event functions and tested it, and it works normally.

        Is there any way to use mouse event function and Itempressed signal together?

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

        @IknowQT said in How to use mouse events and signals together:

        Is there any way to use mouse event function and Itempressed signal together?

        Yes.
        My guess (you did not show any code) that your mouse event handler behaves wrongly (does not propagate the event to base class).

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

        I 1 Reply Last reply
        0
        • jsulmJ jsulm

          @IknowQT said in How to use mouse events and signals together:

          Is there any way to use mouse event function and Itempressed signal together?

          Yes.
          My guess (you did not show any code) that your mouse event handler behaves wrongly (does not propagate the event to base class).

          I Offline
          I Offline
          IknowQT
          wrote on last edited by IknowQT
          #3

          @jsulm

          class usrCntrListWidget : public QListWidget
          {
          	Q_OBJECT
          
          private:
          	bool		m_bClicked;
          	bool		m_bVisibleCheckBox;
          	int			m_nCount;
          	QTimer*		m_pTimer;
          
          public:
          	usrCntrListWidget(QWidget *parent);
          	~usrCntrListWidget();
          	void	VisibleCheckBox(bool bShow);
          
          public slots:
          	void	GetLastTime();
          	void	VisibleCheckBox(QListWidgetItem* item);
          
          protected:
          	void    mousePressEvent(QMouseEvent* event) override;
          	void    mouseReleaseEvent(QMouseEvent* event) override;
          	void    mouseDoubleClickEvent(QMouseEvent* event) override;
          };
          
          
          ------------------------------- cpp -------------------------------
          void usrCntrListWidget::mousePressEvent(QMouseEvent* event)
          {
          	if (event->button() & Qt::LeftButton)
          	{
                  qDebug() << "mousePressEvent";
                  m_pTimer->start(1000);
              }
          }
          
          void usrCntrListWidget::mouseReleaseEvent(QMouseEvent* event)
          {
              if (event->button() & Qt::LeftButton)
              {
                  qDebug() << "mouseReleaseEvent";
          
          		if (m_pTimer->isActive())
                      m_pTimer->stop();
              }
          }
          
          usrCntrListWidget::usrCntrListWidget(QWidget *parent)
          	:   m_bClicked(false),
                  m_nCount(0),
                  m_bVisibleCheckBox(false),
                  m_pTimer(new QTimer(this))
          {
          	this->setMouseTracking(true);
          	this->viewport()->installEventFilter(this);
              this->connect(this->m_pTimer, SIGNAL(timeout()), this, SLOT(GetLastTime()));
              this->connect(this, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(VisibleCheckBox(QListWidgetItem*)));
          }
          
          void usrCntrListWidget::VisibleCheckBox(QListWidgetItem* item) 
          {
              qDebug() << "VisibleCheckBox";
          
          	usrCntrListItem* pItem = qobject_cast<usrCntrListItem*>(this->itemWidget(item));
              pItem->SetCheckVisible(!pItem->GetCheckStatus());
          }
          
          void usrCntrListWidget::mouseDoubleClickEvent(QMouseEvent* event)
          {
              if (event->button() & Qt::LeftButton)
              {
                  qDebug() << "mouseDoubleClickEvent";
          
                  if(m_bVisibleCheckBox)
                      VisibleCheckBox(false);
              }
          }
          
          jsulmJ 1 Reply Last reply
          0
          • I IknowQT

            @jsulm

            class usrCntrListWidget : public QListWidget
            {
            	Q_OBJECT
            
            private:
            	bool		m_bClicked;
            	bool		m_bVisibleCheckBox;
            	int			m_nCount;
            	QTimer*		m_pTimer;
            
            public:
            	usrCntrListWidget(QWidget *parent);
            	~usrCntrListWidget();
            	void	VisibleCheckBox(bool bShow);
            
            public slots:
            	void	GetLastTime();
            	void	VisibleCheckBox(QListWidgetItem* item);
            
            protected:
            	void    mousePressEvent(QMouseEvent* event) override;
            	void    mouseReleaseEvent(QMouseEvent* event) override;
            	void    mouseDoubleClickEvent(QMouseEvent* event) override;
            };
            
            
            ------------------------------- cpp -------------------------------
            void usrCntrListWidget::mousePressEvent(QMouseEvent* event)
            {
            	if (event->button() & Qt::LeftButton)
            	{
                    qDebug() << "mousePressEvent";
                    m_pTimer->start(1000);
                }
            }
            
            void usrCntrListWidget::mouseReleaseEvent(QMouseEvent* event)
            {
                if (event->button() & Qt::LeftButton)
                {
                    qDebug() << "mouseReleaseEvent";
            
            		if (m_pTimer->isActive())
                        m_pTimer->stop();
                }
            }
            
            usrCntrListWidget::usrCntrListWidget(QWidget *parent)
            	:   m_bClicked(false),
                    m_nCount(0),
                    m_bVisibleCheckBox(false),
                    m_pTimer(new QTimer(this))
            {
            	this->setMouseTracking(true);
            	this->viewport()->installEventFilter(this);
                this->connect(this->m_pTimer, SIGNAL(timeout()), this, SLOT(GetLastTime()));
                this->connect(this, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(VisibleCheckBox(QListWidgetItem*)));
            }
            
            void usrCntrListWidget::VisibleCheckBox(QListWidgetItem* item) 
            {
                qDebug() << "VisibleCheckBox";
            
            	usrCntrListItem* pItem = qobject_cast<usrCntrListItem*>(this->itemWidget(item));
                pItem->SetCheckVisible(!pItem->GetCheckStatus());
            }
            
            void usrCntrListWidget::mouseDoubleClickEvent(QMouseEvent* event)
            {
                if (event->button() & Qt::LeftButton)
                {
                    qDebug() << "mouseDoubleClickEvent";
            
                    if(m_bVisibleCheckBox)
                        VisibleCheckBox(false);
                }
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #4

            @IknowQT Yes, you're doing it wrongly.
            Take a look at https://doc.qt.io/qt-5/eventsandfilters.html "Event Handlers".
            " If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class's implementation."
            From that link:

            void MyCheckBox::mousePressEvent(QMouseEvent *event)
            {
                if (event->button() == Qt::LeftButton) {
                    // handle left mouse button here
                } else {
                    // pass on other buttons to base class
                    QCheckBox::mousePressEvent(event); // HERE the base class implementation is called
                }
            }
            

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

            I 1 Reply Last reply
            2
            • jsulmJ jsulm

              @IknowQT Yes, you're doing it wrongly.
              Take a look at https://doc.qt.io/qt-5/eventsandfilters.html "Event Handlers".
              " If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class's implementation."
              From that link:

              void MyCheckBox::mousePressEvent(QMouseEvent *event)
              {
                  if (event->button() == Qt::LeftButton) {
                      // handle left mouse button here
                  } else {
                      // pass on other buttons to base class
                      QCheckBox::mousePressEvent(event); // HERE the base class implementation is called
                  }
              }
              
              I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #5

              @jsulm

              I looked into the function of the list widget, but there is a function that gets the widget item with the mouse position information, so I was able to implement the function I wanted using it.

              void usrCntrListWidget::mousePressEvent(QMouseEvent* event)
              {
              	if (event->button() & Qt::LeftButton)
              	{
                      qDebug() << "mousePressEvent";
                      m_pTimer->start(1000);
              
                      QListWidgetItem* pItem = static_cast<QListWidgetItem*>(itemAt(event->pos()));
              		if (pItem)
                      {
                          usrCntrListItem* p = qobject_cast<usrCntrListItem*>(this->itemWidget(pItem));
                          if (p && p->GetCheckVisible())
              				p->SetCheckStatus(!p->GetCheckStatus());
                      }
                  }
              }
              

              Are there any problems that may arise when implementing the function?

              Christian EhrlicherC 1 Reply Last reply
              0
              • I IknowQT

                @jsulm

                I looked into the function of the list widget, but there is a function that gets the widget item with the mouse position information, so I was able to implement the function I wanted using it.

                void usrCntrListWidget::mousePressEvent(QMouseEvent* event)
                {
                	if (event->button() & Qt::LeftButton)
                	{
                        qDebug() << "mousePressEvent";
                        m_pTimer->start(1000);
                
                        QListWidgetItem* pItem = static_cast<QListWidgetItem*>(itemAt(event->pos()));
                		if (pItem)
                        {
                            usrCntrListItem* p = qobject_cast<usrCntrListItem*>(this->itemWidget(pItem));
                            if (p && p->GetCheckVisible())
                				p->SetCheckStatus(!p->GetCheckStatus());
                        }
                    }
                }
                

                Are there any problems that may arise when implementing the function?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @IknowQT said in How to use mouse events and signals together:

                Are there any problems that may arise when implementing the function?

                The same as @jsulm already told you - you don't call the base class...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1

                • Login

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