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 ?
...全文
511 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一下
内容概要:本文系统研究了Picard迭代法在非线性常微分方程参数估计中的应用,深入阐述了该方法的数学原理及其在参数辨识中的收敛性与稳定性优势。通过构建最小化误差的目标函数,并结合数值积分技术,采用迭代方式逐步逼近系统的真实参数值,有效解决了非线性动态系统中因缺乏解析解而难以进行精确建模的问题。文中提供了完整的Matlab代码实现,涵盖模型定义、迭代求解、参数更新与结果可视化等关键环节,增强了方法的可操作性与工程实用性。研究通过典型非线性系统案例验证了算法的有效性,展示了其在科学计算与工程建模中的良好适应性与推广潜力。; 适合人群:具备常微分方程理论、数值分析基础及Matlab编程能力,从事系统建模、参数辨识、动力学仿真等相关方向的研究生、科研人员和工程技术开发者。; 使用场景及目标:①解决实际工程中非线性微分方程模型的未知参数估计问题;②深入理解Picard迭代法在科学计算中的实现机制与数值特性;③为学术论文复现、科研项目开发或课程设计提供可运行、易调试的技术方案与代码参考。; 阅读建议:建议读者结合文中的数学推导与Matlab代码逐行分析,重点关注迭代流程、目标函数构造与数值积分的耦合实现,通过修改模型结构或噪声条件进行扩展实验,以深化对算法鲁棒性与适用边界的理解。配套资源可通过指定公众号和网盘链接获取,推荐同步学习以加速科研进程。

21,503

社区成员

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

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