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 ?
...全文
434 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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一下
The team that brought you the bestselling Beginning iPhone Development is back again for Beginning iOS 7 Development, bringing this definitive guide up-to-date with Apple’s latest and greatest iOS 7 SDK, as well as with the latest version of Xcode. There’s coverage of brand-new technologies, including a new chapter on Apple’s Sprite Kit framework for game development, as well as significant updates to existing material. You’ll have everything you need to create your very own apps for the latest iOS devices. Every single sample app in the book has been rebuilt from scratch using latest Xcode and the latest 64-bit iOS 7-specific project templates, and designed to take advantage of the latest Xcode features. Assuming only a minimal working knowledge of Objective-C, and written in a friendly, easy-to-follow style, Beginning iOS 7 Development offers a complete soup-to-nuts course in iPhone, iPad, and iPod touch programming. The book starts with the basics, walking through the process of downloading and installing Xcode and the iOS 7 SDK, and then guides you though the creation of your first simple application. From there, you’ll learn how to integrate all the interface elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders. You’ll master a variety of design patterns, from the simplest single view to complex hierarchical drill-downs. The confusing art of table building will be demystified, and you’ll learn how to save your data using the iPhone file system. You’ll also learn how to save and retrieve your data using a variety of persistence techniques, including Core Data and SQLite. And there’s much more! What you’ll learn Everything you need to know to develop your own bestselling iPhone and iPad apps Filled with examples Best practices for optimizing your code and delivering great user experiences What data persistence is, and why it’s important Get started with building cool, crisp user interfaces What and how to use Table Views How to draw to the screen using Core Graphics What geo app development features the new iOS brings to the iPhone How to get your app to work with iCloud and more Who this book is for This book is for aspiring iPhone app developers, new to the iOS SDK. Some prior experience with Objective-C is recommended but not required. Table of Contents Chapter 1: Welcome to the Jungle Chapter 2: Appeasing the Tiki Gods Chapter 3: Handling Basic Interaction Chapter 4: More User Interface Fun Chapter 5: Autorotation and Autosizing Chapter 6: Multiview Applications Chapter 7: Tab Bars and Pickers Chapter 8: Introduction to Table Views Chapter 9: Navigation Controllers and Table Views Chapter 10: Collection View Chapter 11: iPad Considerations Chapter 12: Application Settings and User Defaults Chapter 13: Basic Data Persistence Chapter 14: Documents and iCloud Chapter 15: Grand Central Dispatch, Background Processing, and You Chapter 16: Drawing with Core Graphics Chapter 17: Getting Started with Sprite Kit Chapter 18: Taps, Touches, and Gestures Chapter 19: Where Am I? Finding Your Way with Core Location and Map Kit Chapter 20: Whee! Gyro and Accelerometer! Chapter 21: The Camera and Photo Library Chapter 22: Application Localization Book Details Title: Beginning iOS 7 Development: Exploring the iOS SDK Author: David Mark, Fredrik Olsson, Jack Nutting, Jeff LaMarche Length: 720 pages Edition: 1 Language: English Publisher: Apress Publication Date: 2014-03-19 ISBN-10: 143026022X ISBN-13: 9781430260226
Installing c++/g++ on Windows Disclaimer: This page is being maintained mainly for my students. Use these instructions at your own risk. There is no warranty in any form or shape whatsoever!. There is no guarantee that these instructions are up-to-date. With that understood you may continue with the rest of this page if you choose to accept these terms. This page was last updated on September 13, 2005, but still good as of April 30, 2009. Follow these steps to install g++ (the GNU C++ compiler) for Windows. There is no room for creativity here; you must follow the directions exactly. Pick the drive and a folder in which you want to install g++. I'll assume that it is C:, but you can choose a different one. If you choose a different drive or a different folder, you'll need to adapt the directions below accordingly. Download full.exe, an about 14 megabyte executable, to C:\full.exe by right-clicking on the link. Use Save Link As... or Save Target As... Be sure the browser saves the file as C:\full.exe. Run the downloaded executable. This will install g++ (and a lot of other things that you don't really need) on your hard drive. Go to the C: drive using Windows Explorer and double-click on full.exe. Or, open a DOS window (Start > Programs > Command Prompt), connect to the C: drive using the cd command, and type full. Locate where the bin folder was created for the g++ installation. On my Windows XP machine, it was created in the following path: C:\cygnus\cygwin-b20\H-i586-cygwin32\bin You now should add it to the PATH environment variable. You do that by following: Start -> Control Panel -> System -> Advanced -> Environment Variables At this point you can see the PATH variable either in the User Variables or in the System Variables. Add the g++ path into the PATH variable. You add it to the end of the existing value separated by a semicolon (';'). Make sure that you do not lose the original value. You are just appending more to the end separated by a semicolon. Restart your computer. A Cygnus Solutions entry will appear in your Programs menu, and an icon may appear on your desktop. Don't use them! You will use it using the g++ command on a DOS prompt as explained below. You should now be able to run g++ from a DOS (Command Prompt) window. For example, to compile a file called C:\mine\hello.cpp, connect to the C:\mine folder and enter g++ -g hello.cpp -o hello -lm You'll then be able to run the compiled program by entering hello in the DOS window. If you've installed Emacs as described here, you will also be able to run g++ from Emacs. If, when you do this, Emacs tries to compile with the command make -k, you made a mistake during the Emacs installation. If you want to learn how to run g++ on emacs, see here. If you'd like to learn more about where this free compiler came from, we downloaded it from an older site of http://sourceware.org/cygwin/. If you wish to clean up a little, you may delete the file: full.exe at this point. Your g++ compiler is installed under C:\cygnus.

21,437

社区成员

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

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