Results 1 to 11 of 11

Thread: DoubleClick functionality in TreeWidget which is uses a delegate when hover over item

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    4
    Qt products
    Qt4

    Default Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over

    here is all the code to reproduce the problem: SlotItemPressed() will never be called when double clicking the row:

    Qt Code:
    1. // MyTreeWidget.h
    2. #pragma once
    3. #include <QTreeWidget>
    4. #include <iostream>
    5. #include <QDebug>
    6. class QWidget;
    7.  
    8. class MyTreeWidget : public QTreeWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyTreeWidget(QWidget *parent = NULL);
    14.  
    15. void mouseMoveEvent(QMouseEvent *event);
    16.  
    17. public slots:
    18. void SlotItemPressed(QTreeWidgetItem *item, int index);
    19. };
    20.  
    21. // MyTreeWidget.cpp
    22. #include "MyTreeWidget.h"
    23. #include <QMouseEvent>
    24. #include <QWidget>
    25.  
    26. MyTreeWidget::MyTreeWidget(QWidget *parent) : QTreeWidget(parent)
    27. {
    28. connect( this, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ),
    29. this, SLOT( SlotItemPressed( QTreeWidgetItem*, int ) ) );
    30.  
    31. setMouseTracking(true);
    32. setEditTriggers(QAbstractItemView::CurrentChanged);
    33. }
    34.  
    35. void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)
    36. {
    37. QTreeWidget::mouseMoveEvent(event);
    38. setCurrentIndex( indexAt(event->pos()));
    39. }
    40.  
    41. void MyTreeWidget::SlotItemPressed(QTreeWidgetItem *item, int index)
    42. {
    43. qDebug() << "I never get here\n";
    44. }
    45.  
    46. #include "moc_MyTreeWidget.cpp"
    47.  
    48. // MyDelegate.h
    49. #pragma once
    50.  
    51. #include <QStyledItemDelegate>
    52.  
    53. class MyDelegate : public QStyledItemDelegate
    54. {
    55. Q_OBJECT
    56. public:
    57. MyDelegate(QObject *parent = 0);
    58.  
    59. public:
    60.  
    61. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    62. const QModelIndex &index) const;
    63. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    64. void setModelData(QWidget *editor, QAbstractItemModel *model,
    65. const QModelIndex &index) const;
    66. };
    67.  
    68. // MyDelegate.cpp
    69.  
    70. #include "MyDelegate.h"
    71. #include <QtGui>
    72.  
    73. MyDelegate::MyDelegate(QObject *parent) : QStyledItemDelegate(parent)
    74. {
    75.  
    76. }
    77.  
    78. void MyDelegate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
    79. {
    80. if(index.column() == 0)
    81. {
    82. QPushButton *btn = qobject_cast<QPushButton*>(editor);
    83.  
    84. model->setData(index, btn->text());
    85. } else {
    86. QStyledItemDelegate::setModelData(editor, model, index);
    87. }
    88. }
    89.  
    90. QWidget * MyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    91. {
    92. if(index.column() == 0)
    93. {
    94. QString s1(index.model()->data(index, Qt::EditRole).toString());
    95. QString s2(index.data(Qt::DisplayRole).toString());
    96. QPushButton *btn = new QPushButton(s1, parent);
    97. return btn;
    98. }
    99. return 0;
    100. }
    101.  
    102. void MyDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
    103. {
    104. QPushButton *btn = qobject_cast<QPushButton*>(editor);
    105. if(!btn)
    106. return;
    107.  
    108. btn->setText(index.model()->data(index, Qt::EditRole).toString());
    109. }
    110.  
    111. // main.cpp
    112. #include <QtGui/QApplication>
    113. #include "MyDelegate.h"
    114. #include "MyTreeWidget.h"
    115.  
    116. int main(int argc, char *argv[])
    117. {
    118. QApplication app(argc,argv);
    119.  
    120. MyTreeWidget* tree = new MyTreeWidget;
    121. tree->setItemDelegate(new MyDelegate(tree));
    122. tree->setColumnCount(1);
    123.  
    124. QTreeWidgetItem *item01 = new QTreeWidgetItem(tree);
    125. item01->setFlags(item01->flags() | Qt::ItemIsEditable);
    126. tree->addTopLevelItem(item01);
    127. item01->setText(0, "item01");
    128.  
    129. QTreeWidgetItem *item02 = new QTreeWidgetItem(tree);
    130. item02->setFlags(item02->flags() | Qt::ItemIsEditable);
    131. tree->addTopLevelItem(item02);
    132. item02->setText(0, "item02");
    133. tree->show();
    134. return app.exec();
    135. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over

    Works fine for me... Unless of course the editor is opened and intercepts the double click but that's rather obvious.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    4
    Qt products
    Qt4

    Default Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over

    Quote Originally Posted by wysota View Post
    Works fine for me... Unless of course the editor is opened and intercepts the double click but that's rather obvious.
    You mean it works for you and SlotItemPressed() gets called? You mentioned earlier: "There is nothing in your delegate that would prevent signals to be delivered." Does the delegate now intercept the double click or deliver it?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over

    Quote Originally Posted by torres View Post
    You mean it works for you and SlotItemPressed() gets called?
    Yes.
    You mentioned earlier: "There is nothing in your delegate that would prevent signals to be delivered." Does the delegate now intercept the double click or deliver it?
    Why would the delegate intercept double click? You'd have to reimplement editorEvent() for that to happen.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 26
    Last Post: 7th January 2016, 20:26
  2. Replies: 0
    Last Post: 10th March 2011, 11:44
  3. TreeWidget item editing.
    By kubas in forum Qt Programming
    Replies: 2
    Last Post: 8th September 2009, 07:42
  4. Replies: 5
    Last Post: 10th August 2009, 10:50
  5. [Style Sheet] QMenu::item:hover bug
    By spud in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2007, 14:11

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.