Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Property editor
Qt 6.11 is out! See what's new in the release blog

Property editor

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 2.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by sitesv
    #1

    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?
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      1
      • sitesvS Offline
        sitesvS Offline
        sitesv
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          sitesvS 1 Reply Last reply
          1
          • SGaistS SGaist

            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.

            sitesvS Offline
            sitesvS Offline
            sitesv
            wrote on last edited by sitesv
            #5

            @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...

            1 Reply Last reply
            0
            • sitesvS Offline
              sitesvS Offline
              sitesv
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • sitesvS Offline
                sitesvS Offline
                sitesv
                wrote on last edited by
                #7

                Some ideas?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by SGaist
                  #8

                  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 ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  sitesvS 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    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 ?

                    sitesvS Offline
                    sitesvS Offline
                    sitesv
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      How do you determine the data's type ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      sitesvS 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        How do you determine the data's type ?

                        sitesvS Offline
                        sitesvS Offline
                        sitesv
                        wrote on last edited by
                        #11

                        @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

                        1 Reply Last reply
                        0
                        • nageshN Offline
                          nageshN Offline
                          nagesh
                          wrote on last edited by
                          #12

                          @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.

                          sitesvS 1 Reply Last reply
                          1
                          • nageshN nagesh

                            @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.

                            sitesvS Offline
                            sitesvS Offline
                            sitesv
                            wrote on last edited by sitesv
                            #13

                            @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;
                            }
                            
                            nageshN 1 Reply Last reply
                            0
                            • sitesvS sitesv

                              @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;
                              }
                              
                              nageshN Offline
                              nageshN Offline
                              nagesh
                              wrote on last edited by
                              #14

                              @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

                              1 Reply Last reply
                              1
                              • sitesvS Offline
                                sitesvS Offline
                                sitesv
                                wrote on last edited by sitesv
                                #15

                                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?

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved