How can we save user date to the QTreeWidgetItem?

yanj20 2010-04-08 04:25:53
In MFC for CTreeControl we have function call SetItemData to set the user
data to a tree items. Like that how can we set the user data to QTreeWidget
? . Can any one help me with the simple code snapshot ?
...全文
506 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aisy_D 2010-04-08
  • 打赏
  • 举报
回复
the sample demo "interview" which locate at $QTDIR$\demos\interview\interview.exe is your friend, please take time to understand it....
yanj20 2010-04-08
  • 打赏
  • 举报
回复
struct JOB{
name;
id;
};

QStringList subfileList;
JOB *pJob = new JOB();; //user data struct
subfileList.append(pJob.name);

//here we save the user data to the var
QTreeWidgetItem *childItem = new QTreeWidgetItem(subfileList);
QVariant var(QVariant::UserType, pJob);
childItem->setData(0,Qt::UserRole,var);
parentItem->addChild(childItem); //here we add the childItem to the QTreeWidget

//here we try to get the data saved in var that connected to the Item
JOB *savedObj = (static_cast<JOB*>(var.data())); //???I couldn't get the values
yanj20 2010-04-08
  • 打赏
  • 举报
回复
thanks Aisy_D,so would you please show me the subparagraph code?
Aisy_D 2010-04-08
  • 打赏
  • 举报
回复
I will upload this demo's source-archive to somewhere and share the URL's link for everybody to download it.
Aisy_D 2010-04-08
  • 打赏
  • 举报
回复
Well, there are sure ways to add your extra customer data to your Qtreewidget.
one of the common approachs is that create your own class which inherits "QDataModel", i.e. QDirModel , and then reimplement public function "columnCount ( const QModelIndex & parent = QModelIndex() ) const" to set up extra columns for holding your custmoer data, and then reimplement two functions "data()" , "setData()" to showi or manuplate your own data, another function headerData() for modifying the showing text on the header row of Qtreewidget, I'v just got this cutomer data work in my demo "BatchRename" ,take a look at the screen snap followed:
dyw 2010-04-08
  • 打赏
  • 举报
回复
可以用setData方法设置任意的数据,除了用Qt::UserRole外,你还可以自己定义Role...
yanj20 2010-04-08
  • 打赏
  • 举报
回复
Thank you,I work on ubuntu linux,and i have try the setData function,but it didn't work.

Q_DECLARE_METATYPE(JOB*) //JOB is user defined data structure
QTreeWidget *pTree = new QTreeWidget();
JOB *pJob = new JOB();
QTreeWidgetItem* item = QTreeWidgetItem(pTree);
item->setText(0,"Test Text");
QVariant var;
qVariantSet(var,pJob);
item->setData(0,QAbstractItemModel::UserRole,var);

like the above method,application cann't save the pJob,Can any one help me with the simple code snapshot ?
Lutx 2010-04-08
  • 打赏
  • 举报
回复
setData(column, Qt::UserRole, yourdata);

bsr2009 2010-04-08
  • 打赏
  • 举报
回复
楼上的方法可以试一下.LZ的平台是用在哪里的?S60? CE?
pornographer 2010-04-08
  • 打赏
  • 举报
回复
看了下好像由函数的

先从qtreewidget 获得qtreewidgetitem的指针

然后用这函数
void QTreeWidgetItem::setData ( int column, int role, const QVariant & value )

这个具体我没用过 随便yy一下
内容概要:本文针对传统三电平并网逆变器在谐波抑制、电网不平衡适应性及动态响应方面的不足,提出一种基于有源中点箝位(ANPC)三电平拓扑的高性能并网控制策略。该策略深度融合双极性倍频脉宽调制(DPWMA)、正负序分离锁相技术与电网电压前馈控制,构建了“精准同步—扰动补偿—优质调制”三位一体的一体化控制体系。依托ANPC拓扑在开关损耗均衡、中点电位稳定和低谐波输出方面的硬件优势,结合DPWMA调制提升等效开关频率、正负序分离实现不平衡电网下的精确锁相、前馈控制克服闭环滞后等先进控制手段,显著改善了系统的稳态电能质量、动态响应速度与复杂工况适应能力。通过多工况仿真验证,该复合策略在稳态运行时可大幅降低总谐波畸变率,在电网不平衡与动态扰动工况下仍能维持并网电流对称、功率平稳及快速恢复能力,展现出优异的综合性能与工程应用潜力。; 适合人群:具备电力电子与电力系统基础知识,从事新能源并网、逆变器控制、微电网或相关领域研究的研发人员及研究生。; 使用场景及目标:① 提升高功率并网逆变器的电能质量与运行稳定性;② 解决电网电压不平衡、畸变等复杂工况下的并网难题;③ 优化动态响应性能,提升系统抗扰能力;④ 为ANPC拓扑与先进控制策略的工程化应用提供技术参考。; 阅读建议:建议结合仿真模型深入理解DPWMA调制、正负序分离锁相与前馈控制的实现细节,重点关注多工况下的性能对比分析,以掌握复合控制策略的设计逻辑与优化效果。

21,503

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧