Set a QStandartItem as Expandable without having a child item

335 views Asked by At

i am trying to send a folder structure between two differnt programs which can be on different computers. On my server I have a QFileSystemModel and on my client I have a QTreeView which has a QStandardItemModel as a model. And i have a prebuild signal/slot system which can send QString and QStringList between the programs.

Client:

auto *m_stdItemModel = new QStandardItemModel(this);

auto *m_treeView = new QTreeView(this);
m_treeView->setModel(m_stdItemModel);

I want to sent the child directories from the server everytime I click on the expand button on the client m_treeView. The problem is that an entry is only expandable when it has a children.

My approach to do so was adding a dummy child and removing it when the user clicks the expand button.

Adding dummy:

void addChildToParent(QString child, QStandardItem *parentItem, bool isExpandable)
{
    auto *childItem = new QStandardItem(child);

    if(isExpandable)
    {
        childItem->appendRow(new QStandardItem("dummy"));
    }

   parentItem->appendRow(childItem);
}

Is there a workaround to do add an expand button without adding a dummy child?

Kind regards

1

There are 1 answers

0
G.M. On BEST ANSWER

I think your best bet would be to override QStandardItemModel::hasChildren...

class item_model: public QStandardItemModel {
  using super = QStandardItemModel;
public:
  virtual bool hasChildren (const QModelIndex &parent = QModelIndex()) const override
    {
      if (const auto *item = itemFromIndex(parent)) {

        /*
         * Here you need to return true or false depending on whether
         * or not any attached views should treat `item' as having one
         * or more child items.  Presumably based on the same logic
         * that governs `isExpandable' in the code you've shown.
         */
        return is_expandable(item);
      }
      return super::hasChildren(parent);
    }
};