how can i get the QTreeWidget which i edit use a delegate?

  • how can i get the QTreeWidgetItem which i edit use a delegate?
    virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const //is the parent here the QTreeWidgetItem??
    virtual void setEditorData(QWidget * editor, const QModelIndex & index) const
    virtual void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    //is there any way to get the QTreeWidgetItem editting use model & index?

    thank you

  • Lifetime Qt Champion


    Out of curiosity, why do you need that item in createEditor ?

  • i need to set it to my own data......

  • Lifetime Qt Champion

    How are you setting these data in the first place ?

  • If you want to follow Model/View pattern you take your data from model.
    You get index which allows you to get it.
    Then you can appropriately fill data in editor you create.
    That is how it was meant to be done,

    If you are trying mixed approach which I would not recommend you are on your own.
    You can always override delegate to work for example with a single QTreeWidget and add a member
    which would keep QTreeWidget pointer, then you can get row/column from index and map it to QWidgtet item.

  • @alex_malyu
    i do have a QTreeWidget pointer, but i just find a protected API itemFromIndex,
    how can i get the item from the index?

    here i am using a delegate...

  • This is C++.
    You can always subclass and either override existing virtual function or create a public function which will call protected function and return you info you need.

    class MyTableWidget : public QTableWidget

     QTableWidgetItem *publicItemFromIndex(const QModelIndex &index) const;


    in cpp

    QTableWidgetItem * MyTableWidget : publicItemFromIndex(const QModelIndex &index) const
    return ItemFromIndex( index ) ;

    But in reality there is very small chance you really need pointer to QTableWidgetItem in createEditor.
    In fact I can bet you do not.
    You have access to control, you have access to the data, You can initialize control from the data.

    Why do you even use delegate?
    If you already use QTableWidget you can query it for item using public
    QTableWidgetItem *item(int row, int column)
    if you use proxy for sorting it might be more complicated, but otherwise
    you can get row and column from inder.row() and index.column().

    SGaist tried to ask you to provide more information for a reason.

    The best way would be to describe what you want to achieve instead of trying something unusual
    and then asking for help to solve secondary problem which would not exist if you used proper approach.

Log in to reply