Results 1 to 2 of 2

Thread: Telling apart single click and double click in QTableView

  1. #1
    Join Date
    Aug 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Telling apart single click and double click in QTableView

    Hi,

    I'm using a QTableView (cannot change to QTableWidget) and need a Delegate (not an editor) to do different actions on single and double mouse clicks. The basic idea I came up with is to start a QTimer on a QEvent::MouseButtonRelease and either stop it on receiving a QEvent::MouseButtonDblClick and depending on the program state maybe open a new QWindow or if no double click event is received to change something on the delegate.

    The other QWindow is started through the doubleClicked signal in a controller class:
    Qt Code:
    1. connect(m_table, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(maybeOpenQWindow(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    This is my Delegate's event handling:

    Qt Code:
    1. bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    2. {
    3. if (QEvent::MouseButtonRelease)
    4. {
    5. qDebug() << "single click";
    6. m_timer->start();
    7. event->accept();
    8. return true;
    9. }
    10. else if (event->type() == QEvent::MouseButtonDblClick)
    11. {
    12. qDebug() << "double click";
    13. m_timer->stop();
    14. event->accept();
    15. return true;
    16. }
    17. else
    18. {
    19. return false;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    I saw in the source code, that QAbstractItemView::mouseDoubleClickEvent is sending out a mouse press event like QWidget::mouseDoubleClickEvent so I overloaded it with an empty function in my QTableView and verified it isn't run.

    But I get mixed results.

    Double click on MyDelegate while it is focused and no new QWindow is started:

    Qt Code:
    1. single click
    2. double click
    3. single click
    To copy to clipboard, switch view to plain text mode 

    Where do I get the excess single click event from?
    (in this case my timer is started again and the undesired single click action is executed, but a double click should be ignored if no new QWindow is started)

    Double click on MyDelegate while it is focused and a new QWindow is started:

    Qt Code:
    1. single click
    2. double click
    To copy to clipboard, switch view to plain text mode 

    Why do those situations behave differently?

    To sum it up:
    I need my delegate to
    * always react on a single click (i.e. repaint differently)
    * either start a new QWindow or ignore a double click, depending on programs state

    Any suggestions how I can achieve this? Completely different approaches are welcome as well.

    Cheers
    jgirlich
    Last edited by jgirlich; 26th February 2013 at 15:45. Reason: opening new QWindow depends on program state

  2. #2
    Join Date
    Aug 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Telling apart single click and double click in QTableView

    Huh, no suggestions? Did I ask my question in a bad way or is there nobody who can explain the Qt event behaviour to me?

    Anyway, best solution I found so far: Using a timer to delay the execution of 'maybeOpenQWindow' so the second single click event goes through and I can stop my m_timer on it.

    Qt Code:
    1. bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    2. {
    3. if (QEvent::MouseButtonRelease)
    4. {
    5. qDebug() << "single click";
    6. if (m_timer->isActive())
    7. m_timer->stop();
    8. else
    9. m_timer->start();
    10. event->accept();
    11. return true;
    12. }
    13. else
    14. {
    15. return false;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    And the slot looks like this:

    Qt Code:
    1. void maybeOpenQWindow(const QModelIndex &index)
    2. {
    3. QTimer::singleShot(75, this, SLOT(reallyMaybeOpenQWindow()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    I know it's really dirty, but since I don't seem to understand the event inside a QTableView and it's delegates it's the only way I found to get it working.

Similar Threads

  1. Replies: 2
    Last Post: 16th July 2012, 13:40
  2. Replies: 7
    Last Post: 26th April 2012, 15:45
  3. Replies: 6
    Last Post: 5th June 2009, 10:38
  4. Replies: 2
    Last Post: 12th January 2009, 00:24
  5. Replies: 5
    Last Post: 12th January 2006, 16: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.