number of event filter calls
Unsolved
General and Desktop
-
I checked the number of calls to debug in the event filter, but it came in twice. Why is it called twice?
bool usrCntrListWidget::eventFilter(QObject* dest, QEvent* event) { if (event->type() == QEvent::TouchBegin) { QTouchEvent* e = static_cast<QTouchEvent*>(event); if (e) { if (e->touchPointStates() == Qt::TouchPointPressed) { m_bClicked = true; } else if (e->touchPointStates() == Qt::TouchPointReleased) { m_bClicked = false; } qDebug() << "eventFilter" + m_bClicked; } } else if (event->type() == QEvent::MouseButtonPress) { QMouseEvent* e = static_cast<QMouseEvent*>(event); if (e->button() & Qt::LeftButton) { //if(!m_bVisibleCheckBox) // QTimer::singleShot(1000, this, SLOT(GetLastTime())); m_bClicked = true; //m_tPushTime->start(1000); qDebug() << QString("MouseButtonPress : %1").arg(m_bClicked); } } else if (event->type() == QEvent::MouseButtonRelease) { QMouseEvent* e = static_cast<QMouseEvent*>(event); if (e->button() & Qt::LeftButton) { m_bClicked = false; qDebug() << QString("MouseButtonRelease : %1"); } } else if (event->type() == QEvent::MouseButtonDblClick) { QMouseEvent* e = static_cast<QMouseEvent*>(event); if (e->button() & Qt::LeftButton) { m_bClicked = false; if (m_bVisibleCheckBox) VisibleCheckBox(false); qDebug() << QString("MouseButtonDblClick : %1"); } } return false; }
-
Hi,
Did you install an event filter ?
On which object ?
You should also print the object that is passed in parameter. -
I changed the event filter to mouse event and it worked as I wanted.
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); } }
But can you check if my method is correct?
Others say they didn't call the base class, I don't know what the problem is -
@IknowQT In this thread https://forum.qt.io/topic/132824/how-to-use-mouse-events-and-signals-together I gave you a link which explains how to implement event handlers. Did you read it?