Using QT Tree Model with Custom data

Asked by At

This is my Header file for Container object.

class Container
 {
   private:
   std::string stdstrContainerName;
   std::string stdstrPluginType;
   int iSegments;
   float fRadius;

 public:
     Container();
     Container(std::string , std::string ,  int , float);
     Container(const  Container& obj);  
     ~Container();
     std::string GetName();
     std::string GetType();
     void SetName(std::string stdstrName);
  };
   Q_DECLARE_METATYPE( Container )

I use Container as the datatype in TreeItem.

class TreeItem
 {
   public:
       explicit TreeItem( const Container &data , TreeItem *parent = 0 );
       ~TreeItem();
       TreeItem *parent();
       TreeItem *child(int iNumber);
       int childCount() const;
       int childNumber() const;
       Container data() const;
       bool setData(const Container &data);
       bool insertChildren(int position, int count );
       bool removeChildren( int position  , int count );

  private:
       QList<TreeItem*> childItems;
       Container itemData;
       TreeItem* parentItem;
  };

The problem is that when i add rows to the Tree Structure the rows gets added but with no text on them. I want to set the data in stdstrContainerName as the data on the row.

if i try to write something it does not stay.

i think the problem is with my functions which set the data to TreeItem object

  Container TreeItem::data() const
   {
      return itemData;
    }

    bool TreeItem::setData(const Container &data , QVariant value )
      {
        itemData = data;
        itemData.SetName(value.toString().toStdString() );
        return true;
     }

/

Data Function for Tree Model

  QVariant TreeModel::data(const QModelIndex &index, int role) const
  {
    if (!index.isValid())
    return QVariant();

      if (role != Qt::DisplayRole && role != Qt::EditRole)
        return QVariant();

       TreeItem *item = getItem(index);
         return QString::fromStdString(item->data().GetName());
  }

  bool TreeModel::setData(const QModelIndex &index, const QVariant &val, int 
  role )
  {
     if (role != Qt::EditRole)
       return false;

      Container c = val.value<Container>();
      TreeItem *item = getItem(index);
      bool result = item->setData(c);

     if (result)
      emit dataChanged(index, index, { role });

     return result;
 }

I can also post any additional code of the project.

1 Answers

1
Martin Hennings On

What's missing here is the code of your view.

A QAbstractItemView derived view cannot display Container QVariants by itself.

  • Either you implement your view in such a way that it
    • interprets Container-QVariants and displays e.g. its GetName()
    • provides an editor that can manipulate Container-QVariants
  • or (simpler method) you shift the concretion layer into your model and
    • return item->GetName(), ->GetType() depending on role and index.column()
    • manipulate the Container instance in the TreeItem from the text passed in QVariant val