Reputation: 11651
Is there any way to assign a unique key to an entry in a QStandardItemModel
so that we can check for the presence of that key. If it is present we get the relevant QstandardItem
?
Update:
Here is what I am trying to do. I have 3 column in my table so so i have 3 QStandardItem
.
This is the code I am using:
QStandardItem* item0 = new QStandardItem("Column1");
QStandardItem* item1 = new QStandardItem("Column2");
QStandardItem* item2 = new QStandardItem("Column3");
Now my model is called model
and I am attaching these to my model as such
moddel->setItem(0,0,item0);
moddel->setItem(0,1,item1);
moddel->setItem(0,2,item2);
I need to assign a row some unique key so that I could check the model for that key and the model would return the row number. Any suggestions.
Upvotes: 1
Views: 1966
Reputation: 979
The answer by pnezis addresses the storing of a key but not the accessing of a QStandardItem
from the model. I addressed the storing of data with a QStandardItem
by sub classing QStandardItem
as I needed to store a lot of complex data.
To obtain the QStandardItem
from the model you need to create a QModelIndex
instance with the row/column and then call itemFromIndex(index)
on the model.
My example is taken from a selection callback.
QModelIndex& selectedItem = itemsSelected.front();
QStandardItemModel* model = reinterpret_cast<QStandardItemModel*>(tableView->model());
if (nullptr == model)
return;
QStandardItem *item = model->itemFromIndex(selectedItem);
if (nullptr == item)
return ;
Upvotes: 1
Reputation: 12321
You could use the setData
function of QStandardItem
in order to set a custom key for a user defined role, eg
#define MyRole Qt::UserRole + 2
myItem->setData(Qvariant(key), MyRole)
You can get the data of any index in your model by using the data
call.
QVariant d = mymodel->data(anindex, MyRole)
Writing a function that checks if a key exists should be straight forward.
Upvotes: 2