Results 1 to 3 of 3

Thread: How to store/get pointer on QTreeWidgetItem data?

  1. #1
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to store/get pointer on QTreeWidgetItem data?

    I need to store/get pointer to raw data

    here my test code

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include "qttest.h"
    4. #include <string.h>
    5. #include <stdio.h>
    6.  
    7.  
    8. qttest::qttest(QWidget *parent, Qt::WFlags flags)
    9. : QDialog(parent, flags)
    10. {
    11. ui.setupUi(this);
    12. ui.treeWidget->setHeaderLabels(QStringList() << "#" << "Name" << "Count" );
    13. QTreeWidgetItem *item =0;
    14. for(int i=0;i<50;i++){
    15. item = new QTreeWidgetItem(ui.treeWidget);
    16. item->setText(0,QString::number(i));
    17. item->setText(1,tr("Item %1").arg(i) );
    18. //item->setToolTip(1,"<b>hello's <br>QT</b>");
    19. unsigned char *data = new unsigned char [1024];
    20. strcpy((char*)data,"Hello world");
    21. item->setData(0,Qt::UserRole,data);
    22.  
    23. QString count = QString::number(rand()%100);
    24. item->setText(2,count );
    25. ui.treeWidget->addTopLevelItem(item);
    26. }
    27.  
    28.  
    29.  
    30. }
    31.  
    32. qttest::~qttest()
    33. {
    34.  
    35.  
    36. }
    37.  
    38.  
    39. void qttest::on_listWidget_currentRowChanged(int)
    40. {
    41.  
    42. }
    43.  
    44. void qttest::on_treeWidget_doubleClicked(QModelIndex)
    45. {
    46. //QString text = ui.treeWidget->currentItem()->text(1);
    47. char *num =(char*) ui.treeWidget->currentItem()->data(0,Qt::UserRole).value< char* >();
    48.  
    49.  
    50.  
    51. ui.listWidget->addItem(num);
    52. }
    To copy to clipboard, switch view to plain text mode 

    Code can't compile ,here error

    Qt Code:
    1. Error 1 error C2039: 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>' e:\qt\4.2.2\src\corelib\kernel\qmetatype.h 166
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to store/get pointer on QTreeWidgetItem data?

    Here are your solutions:
    - Use void pointers ( really not recommended )
    - Register your custom types with qRegisterMetaType.

    Why don't you use QString instead of const char*? Or is this data a larger buffer stored as char*?

    Regards

  3. The following user says thank you to marcel for this useful post:

    Teerayoot (29th April 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to store/get pointer on QTreeWidgetItem data?

    Don't use QString is your data is not text. Use QByteArray instead.

  5. The following user says thank you to wysota for this useful post:

    Teerayoot (29th April 2007)

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. QDataStream, QTreeWidgetItem, pointer
    By baca in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2007, 16:59
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.