PDA

View Full Version : QTableWidgetItem setData crashes everytime.



Wing_Zero
16th August 2010, 14:05
Hi at all,
I'm trying to simply set a QString in a QTableWidgetItem.



ui->tableWidget->item(0,0)->setData(Qt::DisplayRoleRole,"foo");

But when I do this the little app crashes.

Instead, if I write something before in the same cell by myself, the setData works out.

It sounds like the item object isn't still created when I try to setData. When I before write something in the same cell trought the GUI, the object is created and the the setData istruction works.

Any Idea? that's real strange.

Sorry for my english :(

Gianluigi

Lykurg
16th August 2010, 14:10
It's not strange at all. ui->tableWidget->item(0,0) will return a null pointer, and this is the reason why you app crashes. You first have to add the items, then you can alter it. And by the way better always check the validity of a pointer before using it.

Wing_Zero
16th August 2010, 19:59
Thanks for your answer!
Sorry for the maybe stupid question, but i'm new to Qt.
How can I Add an Item?
The only way it is to create an Item and then add to the QTableWidget with the SetItem method?
There's no faster way?

And, after that, if I had to create an item by myself, what is the difference between the cells and items?

Thanks in advance.

Gianluigi