Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Counter of touches
Qt 6.11 is out! See what's new in the release blog

Counter of touches

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 691 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.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by 8Observer8
    #1

    Hello. I try to make a simple example that shows a counter of touches:

    13169593-4a33-48f8-9517-386392b3677c-image.png

    I set this attribute:

    setAttribute(Qt::WA_AcceptTouchEvents);
    

    I built the APK file and ran it on Android but the counter of touches isn't changed:

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QtGui/QTouchEvent>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QWidget>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    
    protected:
        bool event(QEvent *event) override;
    
    private:
        QLabel *m_counterOfTouchesLabel;
        int m_counterOfTouches = 0;
    };
    #endif // WIDGET_H
    

    widget.cpp

    #include "widget.h"
    #include <QtWidgets/QHBoxLayout>
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setWindowTitle("Touch Coords");
        resize(300, 300);
        setAttribute(Qt::WA_AcceptTouchEvents);
    
        QHBoxLayout *hbox = new QHBoxLayout(this);
        m_counterOfTouchesLabel = new QLabel("Counter of touches: 0");
        hbox->addWidget(m_counterOfTouchesLabel);
        setLayout(hbox);
    }
    
    Widget::~Widget()
    {
    }
    
    bool Widget::event(QEvent *event)
    {
        switch (event->type())
        {
            case QEvent::TouchBegin:
                m_counterOfTouches++;
                m_counterOfTouchesLabel->setText("Counter of touches:" +
                    QString::number(m_counterOfTouches));
                break;
            default:
                break;
        }
    
        return true;
    }
    
    1 Reply Last reply
    0
    • 8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by
      #2

      Solution:

      bool Widget::event(QEvent *event)
      {
          if (event->type() == QEvent::TouchBegin)
          {
              m_counterOfTouches++;
              m_counterOfTouchesLabel->setText("Counter of touches: " +
                  QString::number(m_counterOfTouches));
              return true;
          }
      
          return QWidget::event(event);
      }
      

      touch-coords-qt6-cpp.gif

      1 Reply Last reply
      1
      • 8Observer88 8Observer8 has marked this topic as solved on

      • Login

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