I have a QTableWidget which I've populated with instances of CustomCellModel; this is working insofar as when I show this table, I see that my custom paint function is working and all cells are displaying properly.
My problem is changing the data that each cell holds after they are in the table. Specifically, I cannot retrieve any data from the table.
As you read my code, you may feel I have used hacks to get things to work. You are correct. I'm still learning Qt.
I put each cell into my table thus:
|
Source code
|
1
2
3
4
5
6
7
|
QTableWidgetItem *twi = new QTableWidgetItem;
CustomCellModel *ccm = new CustomCellModel;
twi->setData(0, qVariantFromValue(*ccm)); // data for display
twi->setData(1, qVariantFromValue(*ccm)); // decoration role...when these are both the same it works.
mytableview->setItem(row, col, twi);
|
This was the result of a LOT of trial and error, the result of which is that my table is displaying as I expect. All of my efforts to RETRIEVE data from the table have proven in vain. I'm doing this now (or trying to) through a selection model
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
|
QModelIndexList index_list = selection_model->selectedIndexes(); // This is working fine.
QModelIndex inx;
foreach(index, index_list)
{
int row = index.row();
int col = index.column();
QTableWidgetItem *twi = mytableview->item(row, col);
logfile << twi->column(); // this outputs -1, as if it's not in a table.
CustomCellModel *ccm = twi->data().data(); // ?! here I am very, very confused.
}
|
Perhaps there are just too many pointer indirections here, because my head's spinning in circles. I should be able to access the data contained in a table cell, no? It should be the case that each QTableWidgetItem is a CustomCellModel (if I understood how I set it up correctly), but no matter what permutation or combination of references into the table I try I only get one of two types: QVariant, and void *.
I've tried casting to my CustomCellModel, Q_DECLARE_METATYPE(CustomCellModel) was required to use qVariantFromValue, which I only used to get my CustomCellModel into the table in the first place, but in no way will QVariant give me the CustomCellModel or a pointer to it.
Basically if there is a better way to do all of this, I'm all ears. I'm really quite frustrated.