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 calculate number of mouse clicks in widget?
Forum Updated to NodeBB v4.3 + New Features

How to calculate number of mouse clicks in widget?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.9k Views 1 Watching
  • 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.
  • yuvaramY Offline
    yuvaramY Offline
    yuvaram
    wrote on last edited by
    #1

    I have tried with a sample, but deterministic behavior is observed.

    widget::widget(QWidget *parent) : QWidget(parent)
    {
    m_numMouseClicks = 0;
    this->setFixedSize(600,400);
    m_vBlyt = new QVBoxLayout(this);
    m_lBtest = new QLabel("MOUSE CLICK TEST");
    m_pBgetClicks = new QPushButton("GetClicks");
    m_vBlyt->addWidget(m_lBtest);
    m_vBlyt->addWidget(m_pBgetClicks);
    connect(m_pBgetClicks,SIGNAL(clicked(bool)),this,SLOT(printNumofClicks()));
    }

    bool widget::eventFilter(QObject *watched, QEvent *event)
    {
    if(event->type() == QEvent::MouseButtonPress){
    m_numMouseClicks++;
    return false;
    }else{
    // standard event processing
    return QObject::eventFilter(watched, event);
    }
    }

    void widget::printNumofClicks()
    {
    qDebug()<<Q_FUNC_INFO<<"# num of clicks :"<<m_numMouseClicks<<endl;
    }

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    widget *wobj = new widget;
    a.installEventFilter(wobj);
    wobj->show();

    return a.exec();
    

    }

    output: number of clicks are not in sync

    Yuvaram Aligeti
    Embedded Qt Developer
    : )

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Fuel
      wrote on last edited by
      #2

      Just reimplement this Method http://doc.qt.io/qt-4.8/qwidget.html#mousePressEvent

      And everytime count the click. Its a protected Method from QWidget.

      yuvaramY 2 Replies Last reply
      2
      • F Fuel

        Just reimplement this Method http://doc.qt.io/qt-4.8/qwidget.html#mousePressEvent

        And everytime count the click. Its a protected Method from QWidget.

        yuvaramY Offline
        yuvaramY Offline
        yuvaram
        wrote on last edited by
        #3

        @Fuel
        Thank you @Fuel MousePressEvent is working, but i have doubt, why eventFilter logic is giving different output? Can you explain ?

        Yuvaram Aligeti
        Embedded Qt Developer
        : )

        1 Reply Last reply
        0
        • F Fuel

          Just reimplement this Method http://doc.qt.io/qt-4.8/qwidget.html#mousePressEvent

          And everytime count the click. Its a protected Method from QWidget.

          yuvaramY Offline
          yuvaramY Offline
          yuvaram
          wrote on last edited by
          #4

          @Fuel
          I want to calculate number of mouse clicks in complete application, by using mousePressEvent it gives number of clicks only in widget. widget has Qpushbutton, on button click : mousePressEvent of widget is not called.

          Yuvaram Aligeti
          Embedded Qt Developer
          : )

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Fuel
            wrote on last edited by
            #5

            Dont know if there is a all in one Solution. Maybe you have to count every Event by itself. So you need to count the Events from pressing the QPushButton and shared it with your Application.

            1 Reply Last reply
            0
            • yuvaramY Offline
              yuvaramY Offline
              yuvaram
              wrote on last edited by
              #6

              Hi,
              Thank you.
              Solved by @dheerendra
              bool widget::eventFilter(QObject *watched, QEvent *event)
              {
              if(event->type() == QEvent::MouseButtonPress){
              QMouseEvent mE = dynamic_cast<QMouseEvent>(event);
              if (m_timeStamp!=mE->timestamp()){
              m_timeStamp=mE->timestamp();
              m_numMouseClicks++;
              }
              qDebug() << " Object ="<< watched->objectName() << " Time =" << mE->timestamp() << endl;
              qDebug()<<Q_FUNC_INFO<<m_numMouseClicks<<"#class Name :"<<watched->metaObject()->className()<<endl;
              this->printNumofClicks();
              }
              return QObject::eventFilter(watched, event);
              }

              Yuvaram Aligeti
              Embedded Qt Developer
              : )

              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