Results 1 to 13 of 13

Thread: touch event

  1. #1
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question touch event

    Hi folks.

    I want to capture the "touch press" event - I mean, detect the exact time when the user touches my touch device, but does not lift or move the finger. It would be equivalent to the mouse press event, that is triggered on the exact moment the mouse button is pressed.
    Today, my application detects the press only when, after a few seconds, "a circle" appears on the screen.

    Is there a way to do that?

    I'm using right know the QT 4.6.2 and Windows 7, compiling it with the Visual Studio compiler.

    Thanks in advance for your help

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: touch event

    Well, as you say, it sounds like mousePressEvent is what you need to look at. Perhaps you could use the time() function from QDateTime in there. As for the current behavior, sounds like a timer is being started and if it exceeds a certain threshold before a release event is detected, it draws a circle; you'd have to post the code of your application's mousePressEvent to really get any meaningful feedback however.

    Hope this helps.

  3. #3
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: touch event

    Hi Urthas
    Thanks for your answer...
    However, I'm already using the same event for mouse press and touch press - I believe QT was supposed to convert touch press events into mouse press. The problem is, when I press with the mouse, the event is caught right the way, in the same moment, but when I press with touch, it takes about 4 seconds to capture the event - it captures only when that white circle appears on the screen, in my touch position.
    I don't know if it is a QT 4.6.2 limitation on Windows 7, or if I'm using the wrong event to catch the touch press... I've already tested the "touch press" feature in other development languages (wpf) and it works just as expected, however I still can't make it work on QT...

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: touch event

    Well, first of all, if you haven't already, I think it prudent to read the documentation for the QTouchEvent class thoroughly. It will tell you what hoops you need to jump through to make a widget respond to touch properly, and there is even a section on event handling. Another potentially useful thing to be aware of is the QEvent::Type enum.

  5. #5
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: touch event

    Well, I did a research for the last 4 hours and I still didn't get the answer . I've found the documentation of QTouchEvent and I've read how it works, but the code I've written didn't work as I expected.
    I found this example, that might explain my problems:
    http://doc.qt.nokia.com/4.6/multitouch-fingerpaint.html
    I've followed the example, but trying to get the event on touch begin (That I thought it would be "the moment when the finger touches the screen) and without ignoring "TouchPointStationary", because that's just what I want to grab (single press, no movement). I've got the event twice - when I've released the finger from the screen (I thought the release was sopposed to be "touch end" ) and after about 4 seconds, when the circle pointing my finger appears.

    So, my conclusion until now is: Mouse press events and QTouchEvents don't work for my needs. Do I have any other option? Maybe if it is a bug in version 4.6.2 and it is fixed in 4.7, I could try a newer version. Is it fixed in 4.7?

  6. #6
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: touch event

    post your code?

  7. #7
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: touch event

    Se the link on my last post and copy all .h, .pro files, also main.cpp and mainwindow.cpp
    This one is for scribblearea.cpp. I've only changed a few lines, to get the touch begin...
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "scribblearea.h"
    4.  
    5. ScribbleArea::ScribbleArea(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. setAttribute(Qt::WA_AcceptTouchEvents);
    9. modified = false;
    10.  
    11. myPenColors
    12. << QColor("green")
    13. << QColor("purple")
    14. << QColor("red")
    15. << QColor("blue")
    16. << QColor("yellow")
    17.  
    18. << QColor("pink")
    19. << QColor("orange")
    20. << QColor("brown")
    21. << QColor("grey")
    22. << QColor("black");
    23. }
    24.  
    25. bool ScribbleArea::openImage(const QString &fileName)
    26. {
    27. QImage loadedImage;
    28. if (!loadedImage.load(fileName))
    29. return false;
    30.  
    31. QSize newSize = loadedImage.size().expandedTo(size());
    32. resizeImage(&loadedImage, newSize);
    33. image = loadedImage;
    34. modified = false;
    35. update();
    36. return true;
    37. }
    38.  
    39. bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat)
    40. {
    41. QImage visibleImage = image;
    42. resizeImage(&visibleImage, size());
    43.  
    44. if (visibleImage.save(fileName, fileFormat)) {
    45. modified = false;
    46. return true;
    47. } else {
    48. return false;
    49. }
    50. }
    51.  
    52. void ScribbleArea::clearImage()
    53. {
    54. image.fill(qRgb(255, 255, 255));
    55. modified = true;
    56. update();
    57. }
    58.  
    59. void ScribbleArea::paintEvent(QPaintEvent *event)
    60. {
    61. QPainter painter(this);
    62. const QRect rect = event->rect();
    63. painter.drawImage(rect.topLeft(), image, rect);
    64. }
    65.  
    66. void ScribbleArea::resizeEvent(QResizeEvent *event)
    67. {
    68. if (width() > image.width() || height() > image.height()) {
    69. int newWidth = qMax(width() + 128, image.width());
    70. int newHeight = qMax(height() + 128, image.height());
    71. resizeImage(&image, QSize(newWidth, newHeight));
    72. update();
    73. }
    74. QWidget::resizeEvent(event);
    75. }
    76.  
    77. void ScribbleArea::resizeImage(QImage *image, const QSize &newSize)
    78. {
    79. if (image->size() == newSize)
    80. return;
    81.  
    82. QImage newImage(newSize, QImage::Format_RGB32);
    83. newImage.fill(qRgb(255, 255, 255));
    84. QPainter painter(&newImage);
    85. painter.drawImage(QPoint(0, 0), *image);
    86. *image = newImage;
    87. }
    88.  
    89. void ScribbleArea::print()
    90. {
    91. #ifndef QT_NO_PRINTER
    92. QPrinter printer(QPrinter::HighResolution);
    93.  
    94. QPrintDialog *printDialog = new QPrintDialog(&printer, this);
    95. if (printDialog->exec() == QDialog::Accepted) {
    96. QPainter painter(&printer);
    97. QRect rect = painter.viewport();
    98. QSize size = image.size();
    99. size.scale(rect.size(), Qt::KeepAspectRatio);
    100. painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
    101. painter.setWindow(image.rect());
    102. painter.drawImage(0, 0, image);
    103. }
    104. #endif // QT_NO_PRINTER
    105. }
    106.  
    107. bool ScribbleArea::event(QEvent *event)
    108. {
    109. //code to test "touch press event"
    110. QMessageBox msgBox;
    111. msgBox.setText("Touched!!");
    112. //End of code to test "touch press event" 1st part
    113. switch (event->type()) {
    114. case QEvent::TouchBegin:
    115. //code to test "touch press event"
    116. msgBox.exec ();
    117. break;
    118. //End of code to test "touch press event"
    119. case QEvent::TouchUpdate:
    120. case QEvent::TouchEnd:
    121. {
    122. QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
    123. foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) {
    124. switch (touchPoint.state()) {
    125. case Qt::TouchPointStationary:
    126. // don't do anything if this touch point hasn't moved
    127. continue;
    128. default:
    129. {
    130. QRectF rect = touchPoint.rect();
    131. if (rect.isEmpty()) {
    132. qreal diameter = qreal(50) * touchPoint.pressure();
    133. rect.setSize(QSizeF(diameter, diameter));
    134. }
    135.  
    136. QPainter painter(&image);
    137. painter.setPen(Qt::NoPen);
    138. painter.setBrush(myPenColors.at(touchPoint.id() % myPenColors.count()));
    139. painter.drawEllipse(rect);
    140. painter.end();
    141.  
    142. modified = true;
    143. int rad = 2;
    144. update(rect.toRect().adjusted(-rad,-rad, +rad, +rad));
    145. }
    146. break;
    147. }
    148. }
    149. break;
    150. }
    151. default:
    152. return QWidget::event(event);
    153. }
    154. return true;
    155. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: touch event

    So the problem, then, is that the message box isn't being displayed, i.e., you're not detecting TouchBegin events?
    Last edited by Urthas; 16th August 2010 at 23:41.

  9. #9
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: touch event

    No, the problem is that I want the message to show in the exact time I touch the screen ("touch press", without moving or lifting the finger from the screen), and it just show after about 4s of touch press.

  10. #10
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: touch event

    I note in the QTouchEvent docs that
    The QEvent::TouchBegin event is propagated up the parent widget chain until a widget accepts it with accept(), or an event filter consumes it.
    It seems to me that your filter is consuming it. Perhaps
    Qt Code:
    1. return QWidget::event(event);
    To copy to clipboard, switch view to plain text mode 
    should be the last statement in your method, and not confined to a default case. Unless you are swallowing an event entirely, handler reimplementation is just to do some *additional* work. Alternately, try explicitly calling accept() in the handling code? I wish an expert might step in and educate us both. :P
    Last edited by Urthas; 17th August 2010 at 18:28.

  11. The following user says thank you to Urthas for this useful post:

    leoalvesmachado (17th August 2010)

  12. #11
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: touch event

    Hi,
    I have the sam problem with Qt 4.7.3 and Wondows 7.
    I don't understand, have you solved the problem, leoalvesmachado?

  13. #12
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs down Re: touch event

    No, I didn't.
    I believe it seems to be a Windows issue. Since the project I was working on was cancelled, I'm not looking for a solution to this problem anymore. I didn't test on Windows 8 though...

  14. #13
    Join Date
    Aug 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: touch event

    Hi all,

    not sure anyone resolve the issue,
    I have the same issue here, and it work fine after I disable the "press and hold" feature on the windows's "Pen and Touch" setting.

    hope this will help.

    Rgds
    SianLin

Similar Threads

  1. Replies: 1
    Last Post: 25th March 2011, 11:33
  2. Touch based QT Keyboard
    By deimus in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2010, 18:35
  3. QGraphicsProxyWidget and touch events
    By johnsoga in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2010, 06:53
  4. Qt and touch functionality
    By rishiraj in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 23rd February 2009, 21:29
  5. Touch screen
    By rchaitanya in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 19th January 2009, 13:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.