Results 1 to 2 of 2

Thread: When and where to invoke QAbstractItemView::setIndexWidge?

  1. #1

    Question When and where to invoke QAbstractItemView::setIndexWidge?

    Hi all experts,
    First apologize for my poor English.
    I have searched related threads but still have no solution.

    My goal is to display a customed widget in QTreeView's last column.
    I have two solultions to achieve that:
    1. use QItemDelegate::createEditor, setEditorData, setModelData, updateEditorGeometry...
    2. use QAbstractItemView::setIndexWidge

    Because the UI spec requires that the customed widget should always get displayed, no matter it is in edit status, I prefer the solution 2 - use QAbstractItemView::setIndexWidge.

    Now my question is when and where to invoke QAbstractItemView::setIndexWidge.


    My tree is QTreeView+QAbstractItemModel structure. I subclasses QTreeView andQAbstractItemModel, and I initialize my model's data from a ABClist.

    code:

    Qt Code:
    1. QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    2. const
    3. {
    4. if (!hasIndex(row, column, parent))
    5. return QModelIndex();
    6.  
    7. TreeItem *parentItem;
    8.  
    9. if (!parent.isValid())
    10. parentItem = rootItem;
    11. else
    12. parentItem = static_cast<TreeItem*>(parent.internalPointer());
    13.  
    14. TreeItem *childItem = parentItem->child(row);
    15. if (childItem)
    16. {
    17. QModelIndex index;
    18. index=createIndex(row, column, childItem);
    19.  
    20. return index;
    21. }
    22. else
    23. return QModelIndex();
    24. }
    25.  
    26. QModelIndex TreeModel::parent(const QModelIndex &index) const
    27. {
    28. if (!index.isValid())
    29. return QModelIndex();
    30.  
    31. TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    32. TreeItem *parentItem = childItem->parent();
    33.  
    34. if (parentItem == rootItem)
    35. return QModelIndex();
    36.  
    37. return createIndex(parentItem->row(), 0, parentItem);
    38. }
    39.  
    40. QVariant TreeModel::data(const QModelIndex &index, int role) const
    41. {
    42. if (!index.isValid())
    43. return QVariant();
    44.  
    45. if (role != Qt::DisplayRole)
    46. return QVariant();
    47.  
    48. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    49.  
    50. return item->data(index.column());
    51. }
    To copy to clipboard, switch view to plain text mode 

    The approach to add data into my model is to add/modify the ABClist and then invoke QAbstractItemModel::reset.

    Now it looks perfect to add data but I don't know where and when to invoke QAbstractItemView::setIndexWidge to set my customed widget. Because I only add/modify data in my model class, but setIndexWidge is in QAbstractItemView class.

    I tried to rewrite QAbstractItemView::dataChanged and QAbstractItemView::rowsInserted, in these two virtual functions, I want to invoke QAbstractItemView::setIndexWidge to set my customed widget, but these two virtual functions never invoked when I modify ABClist or invoke QAbstractItemModel::reset. Why???

    Any idea when and where to invoke QAbstractItemView::setIndexWidge?
    Thanks in advance.

    Last edited by wysota; 3rd December 2009 at 09:48. Reason: missing [code] tags

  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: When and where to invoke QAbstractItemView::setIndexWidge?

    You may invoke the method whenever you want after a particular row/column in the model has been created. Just remember the widget will have nothing in common with your model - it will just be a widget placed in another widget that you will have to control (and synchronize with the model) all by yourself.
    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.


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.