0
我試圖使用simple tree model,但我無法使我的項目中的示例工程。當子類化QAbstractItemModel,沒有顯示在QTreeView
首先,我可以在沒有問題的情況下使用QTreeView和QStandardItemModel。在以下情況下,我可以看到我的QTreeView裏面有數據。請參閱工作代碼:
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;
}
}
現在,下面的代碼不起作用。我正在試圖對模型進行子類化。在那種情況下,QTreeView顯示出來,但完全是空的。有些東西我猜我不明白。
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()));
}
}