Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Property editor



  • Hi!
    Trying to do something like qt creator property editor.
    As I know, there are few variants:

    1. QTreeWidget + setItemWidget
    2. QTreeView + setItemDelegate
    3. QTreeView + setIndexWidget
      What is the best way?

  • Lifetime Qt Champion

    Hi
    The best way is 2 as delegates are much lighter than setItemWidget.
    But if only a few rows are needed then all of them will work.

    That said, you could also be inspired by others
    https://github.com/abhijitkundu/QtPropertyBrowser



  • Trying to understand how to use Qt::UserRole...

    const int MyTypeRole = Qt::UserRole + 1;
    
    enum DelegateType{
        DT_Text,
        DT_Checkbox,
        DT_Combo
    }
    

    Where I have to insert the string like this? :

    item->setData(DT_Checkbox, MyTypeRole);
    

    I using QAbstractItemModel and QTreeView


  • Lifetime Qt Champion

    Hi,

    As silly as may sound: when appropriate.

    When you create the item for example or when you know what to put in that item.



  • @SGaist said in Property editor:

    Hi,

    As silly as may sound: when appropriate.

    When you create the item for example or when you know what to put in that item.

    I have the next incomprehension:
    I fill in a TreeItems structure and after that I execute

    emit dataChanged(QModelIndex(), QModelIndex());
    

    ...for updating view.

    As I understand right, after dataChange(...) execution there is automatic start execution of model functions data(...), flags(...), headerData(..) for connection data with the model.

    I tryed to insert index.model()->setData(...) for UserRole into data(...) function, where a node text returns for DisplayRole...
    But there is no access to setData...



  • This post is deleted!


  • Some ideas?


  • Lifetime Qt Champion

    Please show some patience and allow at least 24 hours before bumping your own thread. This is a voluntary driven forum and not all people are living in the same time zone.

    That said, why are you calling setData in your data function ?

    Can you explain when do you use these additional roles ?



  • @SGaist said in Property editor:

    Please show some patience and allow at least 24 hours before bumping your own thread. This is a voluntary driven forum and not all people are living in the same time zone.

    That said, why are you calling setData in your data function ?

    Can you explain when do you use these additional roles ?

    Hi!
    Thank you for the explanation.
    I trying to create something like a simple property editor with QTreeView + QAbstractItemModel + QItemDelegate.
    The example, that was introduced by @mrjj is complicated for me.
    I have a data structure.
    I need to create a QComboBox, QCheckBox, or QLineEditor delegate for one column of QTreeView depending on the data's item type.
    I really don't understand how to register new role for my createEditor method of my delegate.


  • Lifetime Qt Champion

    How do you determine the data's type ?



  • @SGaist said in Property editor:

    How do you determine the data's type ?

    By "type" variable from my data structure.
    1 - QLineEditor
    2 - QCheckBox
    3 - QComboBox



  • @sitesv I hope, you are experimenting somewhat similar to the example "SpinBoxDelegate".

    Populate the model data using setdata function, provide data for edit role and user role also.

    model.setData(index, DT_Checkbox, Qt::UserRole);
    

    In your case

    I need to create a QComboBox, QCheckBox, or QLineEditor delegate for one column of QTreeView depending on the data's item type.
    

    In example SpinBoxDelegate function

    QWidget *SpinBoxDelegate::createEditor
    

    creates delegate widget QSpinBox.

    Similar to that based on "QModelIndex index" retrive the type of data and create the delegate widget.
    eg:
    int type = index.model()->data(index, Qt::UserRole).toInt();

    now based on the type create and return the widget Checkbox, combo etc.

    follow the similar approach in setEditorData, setModelData function.



  • @nagesh said in Property editor:

    @sitesv I hope, you are experimenting somewhat similar to the example "SpinBoxDelegate".

    Populate the model data using setdata function, provide data for edit role and user role also.

    model.setData(index, DT_Checkbox, Qt::UserRole);
    

    In your case

    I need to create a QComboBox, QCheckBox, or QLineEditor delegate for one column of QTreeView depending on the data's item type.
    

    In example SpinBoxDelegate function

    QWidget *SpinBoxDelegate::createEditor
    

    creates delegate widget QSpinBox.

    Similar to that based on "QModelIndex index" retrive the type of data and create the delegate widget.
    eg:
    int type = index.model()->data(index, Qt::UserRole).toInt();

    now based on the type create and return the widget Checkbox, combo etc.

    follow the similar approach in setEditorData, setModelData function.

    Thank you!
    But I don't understand how to create an 'index' while populating the model via the setData function...
    Could you please explain this?

    I have been trying to do something like this:

    QModelIndex idx = model->index(0,0,QModelIndex());
    setData(idx, "test1", Qt::DisplayRole);
    setData(idx, 111, Qt::UserRole);
    

    but idx.isValid() returns 'false'

    UPD:
    First of all, I should create tree-style data structure of TreeItems.
    For each TreeItem, I need to calculate the index and use setData.
    Is I understand right?

    int TreeModel::rowCount(const QModelIndex &parent) const
    {
        TreeItem *parentItem;
    
        if (!parent.isValid())
            parentItem = rootItem;
        else
            parentItem = static_cast<TreeItem*>(parent.internalPointer());
    
        return parentItem->childCount();
    }
    
    int TreeModel::columnCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
        return rootItem->columnCount();
    }
    
    QVariant TreeModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role != Qt::DisplayRole)
            return QVariant();
    
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        if(role == Qt::DisplayRole){
            return item->data(index.column());
        }
        return QVariant();
    }
    
    bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role){
        if (!index.isValid())
            return false;
        if(role == Qt::DisplayRole){
            TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
            bool result = item->setData(index.column(), value);
            if (result) {
                emit dataChanged(index, index);
                return true;
            }
        }
        return false;
    }
    


  • @sitesv

    First of all, I should create tree-style data structure of TreeItems.
    For each TreeItem, I need to calculate the index and use setData.
    Is I understand right?
    

    Absolutely right.. refer editabletreemodel example project...
    In the tree model override index function



  • How to make QComboBox or QCheckBox delegates show constantly?
    They appear when I clicking on the cell only.
    Or this approach (QTreeView + custom delegates) is not suitable for my case?


Log in to reply