PDA

View Full Version : Usage of QT::UserRole in Qtreewidgetitem



h4harman
18th August 2015, 18:13
Hi,

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

Here's my code :



UGS::AsciiString Hdrstr = "Header";
QString str = QString::fromUtf8(Hdrstr.c_str());
QVariant Headerqv(str);

RootWidget->setData(0, Qt::UserRole + 1, Headerqv);



And finally retrieving the value:



StrValue = Widgetitem->data(0, Qt::UserRole + 1);
QString strval = StrValue.toString();

if (strval.toStdString() == "Header")


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 !

anda_skoa
18th August 2015, 22:01
Are RootWidget and Widgetitem the same pointer?

Cheers,
_

h4harman
19th August 2015, 01:24
They are different pointers but they are pointing to the same item. So, yeah they got same value.

But even I do:


RootWidget->setData(0, Qt::UserRole + 1, Headerqv);

QVariant StrValue = RootWidget->data(0, Qt::UserRole + 1);
QString strval = StrValue.toString();
string str = strval.toStdString();


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

Not sure why.

Thanks,

anda_skoa
19th August 2015, 10:50
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?


But even I do:


RootWidget->setData(0, Qt::UserRole + 1, Headerqv);

QVariant StrValue = RootWidget->data(0, Qt::UserRole + 1);
QString strval = StrValue.toString();
string str = strval.toStdString();


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,
_

h4harman
19th August 2015, 18:37
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 :




QVariant Headerqv = QVariant("Header");
RootWidget->setData(0, Qt::UserRole, Headerqv);

QVariant StrValue = RootWidget->data(0, Qt::UserRole);
QString strval = StrValue.toString();
string str = strval.toStdString();


But thanks for all the help :)

Regards,

h4harman
20th August 2015, 15:47
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.