Results 1 to 11 of 11

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

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

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

    I have a treeWidget which displays the items as buttons when hover over them. I use a custom class derived from QStyledItemDelegate to do that.
    The problem is that I also need the functionality of doubleClick on an item. SlotItemPressed() never gets called the way it is now. Is there a possibility to do both?

    Qt Code:
    1. class MyTreeWidget : public QTreeWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyTreeWidget(QWidget *parent = NULL);
    7. ~MyTreeWidget() {}
    8.  
    9. void mouseMoveEvent(QMouseEvent *event);
    10.  
    11. public slots:
    12. void SlotItemPressed(QTreeWidgetItem *item, int index)
    13. {
    14. qDebug() << "slotItemPressed\n";
    15. }
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

    Here is the implementation MyTreeWidget.cpp
    Qt Code:
    1. MyTreeWidget::MyTreeWidget(QWidget *parent) : QTreeWidget(parent)
    2. {
    3. connect( this, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ),
    4. this, SLOT( SlotItemPressed( QTreeWidgetItem*, int ) ) );
    5.  
    6.  
    7. setMouseTracking(true);
    8. setEditTriggers(QAbstractItemView::CurrentChanged);
    9. }
    10.  
    11. void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)
    12. {
    13. QTreeWidget::mouseMoveEvent(event);
    14. setCurrentIndex( indexAt(event->pos()));
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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

    Does anybody have some inputs? Please let me know if my explanation wasn't clear enough. I'm happy to provide more details...

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    I've copy-pasted your code and it works with standard QTreeWidgetItems, maybe post your StyledItemDelegate class as well.
    Here is code I've tested:
    Qt Code:
    1. // test.h
    2. #ifndef TEST_H
    3. #define TEST_H
    4. #include <QtGui>
    5. class MyTreeWidget : public QTreeWidget{
    6. Q_OBJECT
    7. public:
    8. MyTreeWidget( QWidget * parent =NULL);
    9. ~MyTreeWidget(){}
    10.  
    11. void mouseMoveEvent(QMouseEvent *event);
    12.  
    13. public slots:
    14. void SlotItemPressed(QTreeWidgetItem *item, int index)
    15. {
    16. qDebug() << "slotItemPressed" << index;
    17. }
    18. };
    19. #endif
    20.  
    21. // test.cpp
    22. #include "test.h"
    23. MyTreeWidget::MyTreeWidget(QWidget *parent) : QTreeWidget(parent)
    24. {
    25. connect( this, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ),
    26. this, SLOT( SlotItemPressed( QTreeWidgetItem*, int ) ) );
    27.  
    28.  
    29. setMouseTracking(true);
    30. setEditTriggers(QAbstractItemView::CurrentChanged);
    31. }
    32.  
    33. void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)
    34. {
    35. QTreeWidget::mouseMoveEvent(event);
    36. setCurrentIndex( indexAt(event->pos()));
    37. }
    38.  
    39. int main(int argc, char *argv[])
    40. {
    41. QApplication a(argc, argv);
    42. MyTreeWidget * w = new MyTreeWidget();
    43. w->addTopLevelItem( new QTreeWidgetItem(QStringList() << "this") );
    44. w->addTopLevelItem( new QTreeWidgetItem(QStringList() << "is") );
    45. w->addTopLevelItem( new QTreeWidgetItem(QStringList() << "test") );
    46.  
    47. w->show();
    48.  
    49. return a.exec();
    50. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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

    Thanks for the reply. The problem is that the delegate is pretty much "in the way" when instantiated while hover over. The underlying QTreeWidgetItem won't get the double click.

    Here is the StyleDelegate class:
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QStyledItemDelegate>
    4.  
    5. class MyDelegate : public QStyledItemDelegate
    6. {
    7. Q_OBJECT
    8. public:
    9. MyDelegate(QAbstractItemView *view, QObject *parent = 0);
    10. ~MyDelegate() {}
    11. public:
    12. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    13. const QModelIndex &index) const;
    14. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    15. void setModelData(QWidget *editor, QAbstractItemModel *model,
    16. const QModelIndex &index) const;
    17. private:
    18. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MyDelegate.h"
    2. #include <QtGui>
    3.  
    4. MyDelegate::MyDelegate(QAbstractItemView *v, QObject *parent) : QStyledItemDelegate(parent), view(v)
    5. {
    6. }
    7.  
    8. void MyDelegate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
    9. {
    10. if(index.column() == 0)
    11. {
    12. QPushButton *btn = qobject_cast<QPushButton*>(editor);
    13.  
    14. model->setData(index, btn->text());
    15. } else {
    16. QStyledItemDelegate::setModelData(editor, model, index);
    17. }
    18. }
    19.  
    20.  
    21. QWidget * MyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    22. {
    23. if(index.column() == 0)
    24. {
    25. QString s1(index.model()->data(index, Qt::EditRole).toString());
    26. QString s2(index.data(Qt::DisplayRole).toString());
    27. QPushButton *btn = new QPushButton(s1, parent);
    28. return btn;
    29. }
    30. return 0;
    31. }
    32.  
    33.  
    34.  
    35. void MyDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
    36. {
    37. QPushButton *btn = qobject_cast<QPushButton*>(editor);
    38. if(!btn)
    39. return;
    40.  
    41. btn->setText(index.model()->data(index, Qt::EditRole).toString());
    42. }
    To copy to clipboard, switch view to plain text mode 
    And in main(), I call:
    Qt Code:
    1. MyTreeWidget* tree = new MyTreeWidget;
    2.  
    3. tree->setItemDelegate(new MyDelegate(tree));
    4. tree->setColumnCount(1);
    5.  
    6. QTreeWidgetItem *item01 = new QTreeWidgetItem(tree);
    7. item01->setFlags(item01->flags() | Qt::ItemIsEditable);
    8. tree->addTopLevelItem(item01);
    9. item01->setText(0, "item01");
    10.  
    11. QTreeWidgetItem *item02 = new QTreeWidgetItem(tree);
    12. item02->setFlags(item02->flags() | Qt::ItemIsEditable);
    13. tree->addTopLevelItem(item02);
    14. item02->setText(0, "item02");
    15.  
    16. tree->show();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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

    There is nothing in your delegate that would prevent signals to be delivered. You probably have double click enabled as one of the edit triggers for your view. Try changing that and it should work.
    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.


  6. #6
    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

    No, I don't set the only trigger I set is in TreeWidget:
    Qt Code:
    1. setEditTriggers(QAbstractItemView::CurrentChanged);
    To copy to clipboard, switch view to plain text mode 

    Any other ideas?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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

    Please provide a minimal compilable example reproducing the problem.
    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.


  8. #8
    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 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.


  10. #10
    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?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.