PDA

View Full Version : How to access QTableWidget's default items



Wasabi
12th November 2010, 16:00
Whenever QTableWidget is created, it comes with enough default items to fill the number of cells required.

Is there a way of altering these default items? Or does one have to populate it with one's own QTableWidgetItem's?

I've tried using QTableWidget::item(), but it returns a NULL pointer, which the documentation states as meaning it hasn't been set. Which indicates to me that unless the Item is inserted manually, it's not accessible. However, that seems a tad bit silly to me, so I'd like to know if there is another way of accessing these default items.

high_flyer
12th November 2010, 16:04
Whenever QTableWidget is created, it comes with enough default items to fill the number of cells required.
I am not aware of any "default" items that QTableWidget creates on its own.
Could you explain with code what you mean?

Wasabi
12th November 2010, 16:20
Simply the cells which are naturally displayed when you create a QTableWidget without populating it with your own QTableWidgetItem's.

i.e.

int main(int argc,char* argv[])
{
QApplication QtApp(argc,argv)
QTableWidget table(2,3);
table.show();
return QtApp.exec();
}
This presents a 2x3 table of empty cells. The cells are selectable and editable, and are thus (as I call it) populated with default items. My question is: can these items be accessed?

Wasabi
14th November 2010, 11:38
Can someone please advise?

high_flyer
14th November 2010, 14:31
he cells are selectable and editable, and are thus (as I call it) populated with default items.
This is wrong.
The table size (rows and columns) is given, so the table has that size.
The cells are NOT populated with items, which is why you get NULL items when you ask for them.
Once you populate the cells with items you will be able to access these items.

Wasabi
16th November 2010, 00:25
I understand that the table isn't populated with widgets, but it is clearly populated with... something. As I said, that short, four-line code presents a table with cells that are selectable and whose contents are editable. So something is in each of those cells.

Its just that the thing I'm going to populate the list with the most is QLineEdits, so cells that are selectable and editable is exactly what I'm going for. If I could simply access the default somethings that populate the table, I wouldn't have to allocate a few dozen additional items.

However, that firm response does answer my question. Whatever populates the table is clearly out of reach.

high_flyer
16th November 2010, 08:48
but it is clearly populated with... something.
That something is a QTableWidgetItem. (when it is valid)


Whatever populates the table is clearly out of reach.
The cells you see are only an empty shell.
The data model is still empty, and there are NO items in it.
Read thoroughly the QTableWidget docs, you will see its a convenience widget that uses an item based model.
Its a specialized (often used) implementation of the Model/View paradigm, which you can also find in the docs, and see how it work in Qt.
Your main problem is that you mix between concepts.


Its just that the thing I'm going to populate the list with the most is QLineEdits, so cells that are selectable and editable is exactly what I'm going for. If I could simply access the default somethings that populate the table, I wouldn't have to allocate a few dozen additional items.
Read about Model/View in Qt, how a model interacts with data aware Widgeta, and what is the use of delegates.
That will get you on the right track.

NOTE:
Since the trolls made abstraction of the way data aware widgets work, with models, and delegates, it got more complected, but at the same time semantically more correct and more flexible.
For people who need simple item based widget, which is often the case, there are some convenience classes, the as QTableWidget.
But they are nothing more than a specialized implementation of the more abstract classes and concepts, which you have to understand it you want to work with them correctly.