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 ?
...全文
495 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一下
内容概要:本文档系统性地介绍了2024年最新提出的两种智能优化算法——青蒿素优化算法与霜冰优化算法(RIME)的原理、实现方法及其性能对比分析,并提供了完整的Matlab代码实现。文档不仅聚焦于核心算法的仿真与验证,还整合了大量前沿科研资源,涵盖微电网优化、风电功率预测、无人机三维路径规划、电动汽车调度、图像融合、负荷预测、通信信号处理、电力系统故障恢复等多个高价值应用场景。所有案例均基于Matlab/Simulink平台进行建模与仿真,强调算法在复杂工程系统中的实际应用能力,旨在为科研人员提供一套从理论到代码再到应用的完整复现体系。; 适合人群:具备一定编程基础和科研背景的研究生、高校教师及工程技术人员,尤其适合从事智能优化算法研究、新能源系统优化、自动化控制、电力系统调度、无人机导航与路径规划等相关领域的研究人员。; 使用场景及目标:①用于高水平学术论文的复现与创新性研究,提升科研效率与成果产出;②应用于复杂工程系统的建模仿真与智能优化设计,如多能互补系统调度、无人机避障路径规划、微电网能量管理等;③作为智能优化算法的教学与学习资料,深入理解现代元启发式算法的设计思想与实现机制。; 阅读建议:建议读者结合文档中提供的Matlab代码与Simulink仿真模型,按照目录结构循序渐进地学习与实践,优先选择与自身研究方向契合的案例进行代码复现,重点关注算法参数设置、收敛曲线分析与多算法对比实验部分,以全面提升算法应用与科研创新能力。

21,496

社区成员

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

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