Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion net-view/operation/netmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -207,6 +207,8 @@ NetManagerPrivate::NetManagerPrivate(NetManager *manager)
, m_supportWireless(false)
, q_ptr(manager)
{
m_root->item()->setParent(this);
m_deleteItem->item()->setParent(this);
m_root->updateenabled(false);
addItem(m_root, nullptr);
addItem(m_deleteItem, nullptr);
Expand Down
5 changes: 4 additions & 1 deletion net-view/operation/private/netitemprivate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -49,9 +49,10 @@
case NetType::Type: \
netItemPrivate = new Net##Type##Private(); \
netItemPrivate->m_item = new Net##Type(netItemPrivate, id); \
netItemPrivate->m_item->setParent(netItemPrivate); \
break

NetItemPrivate *NetItemPrivate::New(NetType::NetItemType type, const QString &id)

Check warning on line 55 in net-view/operation/private/netitemprivate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'New' is never used.
{
NetItemPrivate *netItemPrivate = nullptr;
switch (type) {
Expand Down Expand Up @@ -121,6 +122,7 @@

Q_EMIT m_item->childAboutToBeAdded(m_item, index);
m_children.insert(m_children.begin() + index, child->item());
child->m_item->setParent(m_item);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Keep QObject parent and logical m_parent in sync or derive one from the other to avoid inconsistencies.

Here you now update QObject parent (child->m_item->setParent(m_item)) and logical parent (child->m_parent = m_item) separately, which risks them getting out of sync if any future reparenting only updates one. Please either derive m_parent from QObject::parent() and drop the manual pointer, or centralize all parent changes in a single helper that updates both together to avoid subtle hierarchy inconsistencies.

Suggested implementation:

    Q_EMIT m_item->childAboutToBeAdded(m_item, index);
    m_children.insert(m_children.begin() + index, child->item());
    child->setParentItem(m_item);
    Q_EMIT m_item->childAdded(child->item());

To fully implement the centralization and keep QObject parent and logical parent in sync, you should also:

  1. In the corresponding header for NetItemPrivate (likely netitemprivate.h), declare a helper method, for example:

    void setParentItem(QObject *parent);

    or, if m_parent is a more specific type, use that type:

    void setParentItem(NetItem *parent);
  2. In net-view/operation/private/netitemprivate.cpp, define this helper so that it updates both the QObject parent and the logical m_parent in one place, e.g.:

    void NetItemPrivate::setParentItem(QObject *parent)
    {
        m_item->setParent(parent);
        m_parent = parent;
    }

    Adjust the parameter type and assignment as needed to match the actual type of m_parent.

  3. Search for any other places in the codebase where both m_item->setParent(...) and m_parent = ... (or equivalent) are called separately, and replace those paired operations with a single call to setParentItem(...) so that all reparenting goes through this centralized helper.


child->m_parent = m_item;
Q_EMIT m_item->childAdded(child->item());
Expand All @@ -137,6 +139,7 @@
Q_EMIT m_item->childAboutToBeRemoved(m_item, it - m_children.begin());
m_children.erase(it);
child->m_parent = nullptr;
child->item()->setParent(nullptr);
Q_EMIT m_item->childRemoved(child->item());
Q_EMIT m_item->childrenChanged();
return true;
Expand Down
Loading