How to use mouse events and signals together
-
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?
-
@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 } }
-
@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). -
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); } }
-
@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 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?
-
@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...