ok another question, I have a combobox delegate, which I want to have the item name in the QComboBox with an ID in it. Heres my current code:
//if its the combobox column:
if (index.column() == 3) {
QString value
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
comboBox->addItem("", "0");
for (int i = 0; i < category->itemList1.size(); ++i) {
comboBox->addItem(category->itemList1.at(i), category->itemList2.at(i));
}
comboBox->setCurrentIndex(comboBox->findText(value));
//if its not a combobox column
} else {
//do stuff for non-combobox delegates
}
}
if (index.column() == 3) {
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
model->setData(index, comboBox->currentText(), Qt::EditRole);
//if its not a combobox column
} else {
//do stuff for non-combobox delegates
}
}
void inventoryDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
//if its the combobox column:
if (index.column() == 3) {
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem("", "0");
for (int i = 0; i < category->itemList1.size(); ++i) {
comboBox->addItem(category->itemList1.at(i), category->itemList2.at(i));
}
comboBox->setCurrentIndex(comboBox->findText(value));
//if its not a combobox column
} else {
//do stuff for non-combobox delegates
}
}
void inventoryDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
if (index.column() == 3) {
QComboBox *comboBox = static_cast<QComboBox*>(editor);
model->setData(index, comboBox->currentText(), Qt::EditRole);
//if its not a combobox column
} else {
//do stuff for non-combobox delegates
}
}
To copy to clipboard, switch view to plain text mode
Notice that in setEditorData, I use:
comboBox->setCurrentIndex(comboBox->findText(value));
comboBox->setCurrentIndex(comboBox->findText(value));
To copy to clipboard, switch view to plain text mode
and in setModelData I use:
model->setData(index, comboBox->currentText(), Qt::EditRole);
model->setData(index, comboBox->currentText(), Qt::EditRole);
To copy to clipboard, switch view to plain text mode
what I would like to be able to do, is when I setData to the model be able to store the QComboBox text/data pair that was selected to the model, so that in setEditorData i could use:
comboBox->setCurrentIndex(comboBox->findData(data));
comboBox->setCurrentIndex(comboBox->findData(data));
To copy to clipboard, switch view to plain text mode
to not only make sure that the correct listing in the combobox is selected when it populates, but have the user be able to see the selected "Text" in the table cell, but the program uses the "data" associated with it for its internal uses.
How can I store data in a QStandardItemModel cell in a similar fashion as the items are in a QComboBox item?
Bookmarks