子类化QTreeWidgetItem实现增加Item的属性

时间:2024-09-02 12:07:02

因为有需求是点击QTreeWidgetItem需要获取该Item的节点的相关属性,Item需要保存关联的属性,那么就需要扩展QTreeWidgetItem,当然,C++中扩展修改一个类或组件的方式就是继承了,

然后创建子类化的类的实例,就可以添加进QTreeWidget了,以下是子类化的代码:

///////////////////////////////H文件/////////////////////////////////
#pragma once #include <QTreeWidgetItem>
#include <QStringList>
#include <QObject> class OrgTreeWidgetItem : public QObject, public QTreeWidgetItem
{ Q_OBJECT
public:
OrgTreeWidgetItem(OrgTreeWidgetItem *parent);
OrgTreeWidgetItem(OrgTreeWidgetItem *parent, const QStringList& list);
OrgTreeWidgetItem(const QStringList& list);
~OrgTreeWidgetItem(); void setOrgId(const QString& id);
void setOrgName(const QString& name);
void setOrgAddress(const QString& addr);
void setOrgDescription(const QString& desc); QString getOrgId() const;
QString getOrgName() const;
QString getOrgAddress() const;
QString getOrgDescription() const;
public:
QString m_orgId;
QString m_orgName;
QString m_orgAddress;
QString m_orgDescription;
}; /////////////////////////////CPP实现文件////////////////////////////
OrgTreeWidgetItem::OrgTreeWidgetItem(OrgTreeWidgetItem *parent)
: QTreeWidgetItem(parent)
{
} OrgTreeWidgetItem::OrgTreeWidgetItem(OrgTreeWidgetItem *parent, const QStringList& list)
: QTreeWidgetItem(parent,list)
{ } OrgTreeWidgetItem::OrgTreeWidgetItem(const QStringList& list)
: QTreeWidgetItem(list)
{ } OrgTreeWidgetItem::~OrgTreeWidgetItem()
{
} void OrgTreeWidgetItem::setOrgId(const QString& id)
{
m_orgId = id;
} void OrgTreeWidgetItem::setOrgName(const QString& name)
{
m_orgName = name;
} void OrgTreeWidgetItem::setOrgAddress(const QString& addr)
{
m_orgAddress = addr;
} void OrgTreeWidgetItem::setOrgDescription(const QString& desc)
{
m_orgDescription = desc;
} QString OrgTreeWidgetItem::getOrgId() const
{
return m_orgId;
} QString OrgTreeWidgetItem::getOrgName() const
{
return m_orgName;
} QString OrgTreeWidgetItem::getOrgAddress() const
{
return m_orgAddress;
} QString OrgTreeWidgetItem::getOrgDescription() const
{
return m_orgDescription;
}

以下是用法:

 void OrgTree::appendChildToTreeItem(OrgTreeWidgetItem* item, const OrgTreeNode& node)
{
OrgTreeWidgetItem* child = new OrgTreeWidgetItem(QStringList() << QString::fromStdString(node.name));
child->setOrgId(QString(node.id.c_str()));
child->setOrgName(QString(node.name.c_str()));
child->setOrgAddress(QString(node.addr.c_str()));
child->setOrgDescription(QString(node.description.c_str()));
item->addChild(child); auto childs = m_parser->findChildsById(node.id);
for (auto childNode : childs)
{
appendChildToTreeItem(child, childNode);
}
}

这样就可以复用QTreeWidget的信号了,单击Item就跳转到相应的槽函数,就为以后弹出上下文菜单增加,删除节点做好了准备:

  connect(ui.treeWidget, &QTreeWidget::itemClicked,
this, &OrgTree::slotItemClicked);
connect(ui.treeWidget, &QTreeWidget::itemDoubleClicked,
this, &OrgTree::slotItemDoubleClicked); void OrgTree::slotItemClicked(QTreeWidgetItem *item, int column)
{
auto test = dynamic_cast<OrgTreeWidgetItem*>(item);
if (test)
{
QString name = test->getOrgName();
QString id = test->getOrgId();
QString addr = test->getOrgAddress();
QString description = test->getOrgDescription(); QString format = QString("name: %1 \r\n id: %2 \r\n address: %3 \r\n description: %4").arg(name)
.arg(id).arg(addr).arg(description); ui.plainTextEdit->setPlainText(format);
}
} void OrgTree::slotItemDoubleClicked(QTreeWidgetItem *item, int column)
{
auto test = dynamic_cast<OrgTreeWidgetItem*>(item);
if (test)
{
QString name = test->getOrgName();
QString id = test->getOrgId(); }
}

references:

https://*.com/questions/28824494/how-to-used-itemexpanded-with-a-subclass-of-qtreewidgetitem