Results 1 to 2 of 2

Thread: QTreeView editor always visible

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeView editor always visible

    Hi all,
    Based on this post :
    http://www.qtcentre.org/threads/2409...-in-QTableView
    There is a new way of achieve that using traditional delegate ?
    Thanks for the help

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTreeView editor always visible

    You can use real widgets applied to the cells as in the thread you linked to. This is OK for small fixed numbers of rows but does not scale well.

    You can achieve a similar effect using a delegate by using the paint() function to draw a static image of the desired widget when the cell is not in edit mode. You could manually draw the equivalent of the widget, or use a real instance of the widget and QWidget::render() it. Here is a primitive example that fakes a spin box editor:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTableWidget>
    3. #include <QStyledItemDelegate>
    4. #include <QScopedPointer>
    5. #include <QSpinBox>
    6. #include <QPainter>
    7.  
    8. class Delegate: public QStyledItemDelegate
    9. {
    10. Q_OBJECT
    11. public:
    12. Delegate(QObject *p = 0):
    13. QStyledItemDelegate(p),
    14. spin(new QSpinBox)
    15. {
    16. }
    17.  
    18. void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    19. {
    20. if (!index.isValid()) return;
    21.  
    22. const int value = index.data(Qt::EditRole).toInt();
    23. spin->setValue(value);
    24. spin->setFrame(false);
    25. spin->resize(option.rect.size());
    26. QPixmap pixmap(option.rect.size());
    27. QPainter p(&pixmap);
    28. spin->render(&p);
    29. p.end();
    30.  
    31. painter->drawPixmap(option.rect, pixmap);
    32. }
    33.  
    34. private:
    35. QScopedPointer<QSpinBox> spin;
    36. };
    37.  
    38.  
    39. int main(int argc, char **argv)
    40. {
    41. QApplication app(argc, argv);
    42.  
    43. QTableWidget t(10, 2);
    44. t.setItemDelegateForColumn(1, new Delegate(&t));
    45. for (int i = 0; i < 10; ++i) {
    46. QTableWidgetItem *item = new QTableWidgetItem(QString("Row %1").arg(i));
    47. t.setItem(i, 0, item);
    48. item = new QTableWidgetItem;
    49. item->setData(Qt::EditRole, i);
    50. t.setItem(i, 1, item);
    51. }
    52. t.show();
    53.  
    54. return app.exec();
    55. }
    56. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    For production use with a complex editor widget you might cache pixmaps.

  3. The following user says thank you to ChrisW67 for this useful post:

    Alundra (13th March 2014)

Similar Threads

  1. Replies: 0
    Last Post: 12th November 2012, 13:12
  2. Replies: 4
    Last Post: 8th February 2012, 13:16
  3. Replies: 0
    Last Post: 7th July 2011, 12:06
  4. Replies: 0
    Last Post: 11th January 2011, 12:31
  5. Replies: 1
    Last Post: 18th September 2009, 20:18

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.