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

admin 2022年9月15日10:49:35安全开发评论6 views3287字阅读10分57秒阅读模式

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

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

///////////////////////////////H文件/////////////////////////////////#pragma once
#include #include #include
class OrgTreeWidgetItem : public QObject, public QTreeWidgetItem{
Q_OBJECTpublic: 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(item); if (test) { QString name = test->getOrgName(); QString id = test->getOrgId(); QString addr = test->getOrgAddress(); QString description = test->getOrgDescription();
QString format = QString("name: %1 rn id: %2 rn address: %3 rn 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(item); if (test) { QString name = test->getOrgName(); QString id = test->getOrgId();
}}

references:

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

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

子类化QTreeWidgetItem实现增加Item的属性 - foo__hack - 博客园 (cnblogs.com)

原文始发于微信公众号(汇编语言):子类化QTreeWidgetItem实现增加Item的属性

特别标注: 本站(CN-SEC.COM)所有文章仅供技术研究,若将其信息做其他用途,由用户承担全部法律及连带责任,本站不承担任何法律及连带责任,请遵守中华人民共和国安全法.
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年9月15日10:49:35
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                  子类化QTreeWidgetItem实现增加Item的属性 http://cn-sec.com/archives/1296924.html

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: