diff options
-rw-r--r-- | examples/opcua/opcuaviewer/treeitem.cpp | 20 | ||||
-rw-r--r-- | examples/opcua/opcuaviewer/treeitem.h | 2 |
2 files changed, 20 insertions, 2 deletions
diff --git a/examples/opcua/opcuaviewer/treeitem.cpp b/examples/opcua/opcuaviewer/treeitem.cpp index 9f4a7d1..b98f9e7 100644 --- a/examples/opcua/opcuaviewer/treeitem.cpp +++ b/examples/opcua/opcuaviewer/treeitem.cpp @@ -169,7 +169,15 @@ TreeItem *TreeItem::parentItem() void TreeItem::appendChild(TreeItem *child) { - mChildItems.append(child); + if (!child) + return; + + if (!hasChildNodeItem(child->mNodeId)) { + mChildItems.append(child); + mChildNodeIds.insert(child->mNodeId); + } else { + child->deleteLater(); + } } QPixmap TreeItem::icon(int column) const @@ -198,6 +206,11 @@ QPixmap TreeItem::icon(int column) const return p; } +bool TreeItem::hasChildNodeItem(const QString &nodeId) const +{ + return mChildNodeIds.contains(nodeId); +} + void TreeItem::startBrowsing() { if (mBrowseStarted) @@ -234,6 +247,9 @@ void TreeItem::browseFinished(QVector<QOpcUaReferenceDescription> children, QOpc auto index = mModel->createIndex(row(), 0, this); for (const auto &item : children) { + if (hasChildNodeItem(item.nodeId())) + continue; + auto node = mModel->opcUaClient()->node(item.nodeId()); if (!node) { qWarning() << "Failed to instantiate node:" << item.nodeId(); @@ -241,7 +257,7 @@ void TreeItem::browseFinished(QVector<QOpcUaReferenceDescription> children, QOpc } mModel->beginInsertRows(index, mChildItems.size(), mChildItems.size() + 1); - mChildItems.append(new TreeItem(node, mModel, item, this)); + appendChild(new TreeItem(node, mModel, item, this)); mModel->endInsertRows(); } diff --git a/examples/opcua/opcuaviewer/treeitem.h b/examples/opcua/opcuaviewer/treeitem.h index 7f97cf5..d67a2fa 100644 --- a/examples/opcua/opcuaviewer/treeitem.h +++ b/examples/opcua/opcuaviewer/treeitem.h @@ -76,6 +76,7 @@ public: TreeItem *parentItem(); void appendChild(TreeItem *child); QPixmap icon(int column) const; + bool hasChildNodeItem(const QString &nodeId) const; private slots: void startBrowsing(); @@ -96,6 +97,7 @@ private: bool mAttributesReady = false; bool mBrowseStarted = false; QList<TreeItem *> mChildItems; + QSet<QString> mChildNodeIds; TreeItem *mParentItem = nullptr; private: |