Results 1 to 6 of 6

Thread: Usage of QT::UserRole in Qtreewidgetitem

  1. #1
    Join Date
    May 2015
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Usage of QT::UserRole in Qtreewidgetitem

    Hi,

    I am having issues with using Qt::UserRole with Qtreewidgetitem.

    Here's my code :

    Qt Code:
    1. UGS::AsciiString Hdrstr = "Header";
    2. QString str = QString::fromUtf8(Hdrstr.c_str());
    3. QVariant Headerqv(str);
    4.  
    5. RootWidget->setData(0, Qt::UserRole + 1, Headerqv);
    To copy to clipboard, switch view to plain text mode 

    And finally retrieving the value:

    Qt Code:
    1. StrValue = Widgetitem->data(0, Qt::UserRole + 1);
    2. QString strval = StrValue.toString();
    3.  
    4. if (strval.toStdString() == "Header")
    To copy to clipboard, switch view to plain text mode 
    In this line :
    if (strval.toStdString() == "Header")

    there's no value of the string. Can anyone suggest what am I doing wrong over here ?

    Thank you !

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Usage of QT::UserRole in Qtreewidgetitem

    Are RootWidget and Widgetitem the same pointer?

    Cheers,
    _

  3. #3
    Join Date
    May 2015
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Usage of QT::UserRole in Qtreewidgetitem

    They are different pointers but they are pointing to the same item. So, yeah they got same value.

    But even I do:
    Qt Code:
    1. RootWidget->setData(0, Qt::UserRole + 1, Headerqv);
    2.  
    3. QVariant StrValue = RootWidget->data(0, Qt::UserRole + 1);
    4. QString strval = StrValue.toString();
    5. string str = strval.toStdString();
    To copy to clipboard, switch view to plain text mode 

    To check the value right after setting the data, str value is still NULL , mean an empty string"" .

    Not sure why.

    Thanks,

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Usage of QT::UserRole in Qtreewidgetitem

    Quote Originally Posted by h4harman View Post
    They are different pointers but they are pointing to the same item.
    If it is not the same pointer, how can it be the same object?

    Quote Originally Posted by h4harman View Post
    But even I do:
    Qt Code:
    1. RootWidget->setData(0, Qt::UserRole + 1, Headerqv);
    2.  
    3. QVariant StrValue = RootWidget->data(0, Qt::UserRole + 1);
    4. QString strval = StrValue.toString();
    5. string str = strval.toStdString();
    To copy to clipboard, switch view to plain text mode 

    To check the value right after setting the data, str value is still NULL , mean an empty string"" .
    Have you checked Headerqv? Does it contain the string?

    Cheers,
    _

  5. #5
    Join Date
    May 2015
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Usage of QT::UserRole in Qtreewidgetitem

    If it is not the same pointer, how can it be the same object?
    Well I needed 2 pointers to point to the same address. So it's the same object coz they are pointing to the same value. I think that's what you wanted to say.

    But anyhow, I got the solution. I was just writing some extra lines of code for no reason. I did the following :


    Qt Code:
    1. QVariant Headerqv = QVariant("Header");
    2. RootWidget->setData(0, Qt::UserRole, Headerqv);
    3.  
    4. QVariant StrValue = RootWidget->data(0, Qt::UserRole);
    5. QString strval = StrValue.toString();
    6. string str = strval.toStdString();
    To copy to clipboard, switch view to plain text mode 

    But thanks for all the help

    Regards,

  6. #6
    Join Date
    May 2015
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Usage of QT::UserRole in Qtreewidgetitem

    If it is not the same pointer, how can it be the same object?
    I think now I know why you pointed that out. So it seems, widgets are individual objects. I can't use another widget item to point to it and use it values. I thought I could do that.

Similar Threads

  1. Replies: 1
    Last Post: 13th February 2015, 07:42
  2. QFileSystemModel + UserRole
    By NIteLordz in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2015, 18:39
  3. Using Qt::UserRole in the Model/View architecture
    By d_stranz in forum Qt Programming
    Replies: 8
    Last Post: 23rd January 2011, 17:13
  4. Replies: 3
    Last Post: 6th October 2010, 17:11
  5. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 10:40

Tags for this Thread

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.