0

I am trying to use simple tree model, but I am not able to make the example works in my project.

First, I was able to use QTreeView without problem, with QStandardItemModel. In the following case, I am able to see my QTreeView with data inside. See the working code :

constructor{
m_pModel = new QStandardItemModel();
ui.treeViewDevicesList->setModel(m_pModel);
fillTreeView(devicesList);
}

void GatewayDeviceViewerEditor::fillTreeView(const std::vector<Payload> &devicesList)
{
    QVector<QStandardItem *> parents(MAX_PARENTS);
    parents[0] = m_pModel->invisibleRootItem();

    for (vector<Payload>::const_iterator it = devicesList.begin() ; it != devicesList.end(); ++it)
    {
        QStandardItem *pTreeViewItem = new QStandardItem();
        string rootTitle =  it->deviceId + " " + it->deviceName + " " + it->status ;
        pTreeViewItem->setText(rootTitle.c_str());
        parents[0]->appendRow(pTreeViewItem);
       // parents[1] = pTreeViewItem;
    }
}

Now, the following code is NOT working. I am trying to subclass the model. In that case, the QTreeView is showing up but completly empty. There is something that I guess that I don't understand.

entryPoint{
DeviceTreeModel deviceTreeModel(devicesList);
ui.treeViewDevicesList->setModel(&deviceTreeModel);
}

DeviceTreeModel::DeviceTreeModel(const std::vector<Payload> &devicesList, QObject *parent)
    : QAbstractItemModel(parent)
{
    QList<QVariant> rootData;
    rootData << "Title" << "Summary";
    m_pRootItem = new DeviceTreeItem(rootData);
    setupModelData(devicesList, m_pRootItem);
}

void DeviceTreeModel::setupModelData(const std::vector<Payload> &devicesList, DeviceTreeItem *parent)
{
    QList<DeviceTreeItem*> parents;
    parents << parent;


    for (vector<Payload>::const_iterator it = devicesList.begin() ; it != devicesList.end(); ++it)
    {
        string rootTitle =  it->deviceId + " " + it->deviceName + " " + it->status ;
        QString test(rootTitle.c_str());
        QList<QVariant> columnData;
        columnData << test << "Summary";

        parents.last()->appendChild(new DeviceTreeItem(columnData, parents.last()));
    }

}
peterphonic
  • 951
  • 1
  • 19
  • 38

1 Answers1

1

In entryPoint, I think that the deviceTreeModel needs to be allocated with new, instead of being a local instance that will be destroyed when exiting the function.

jpo38
  • 20,821
  • 10
  • 70
  • 151
HelperY
  • 11
  • 1