aresa
3rd December 2013, 06:28
Hi all,
I use QStandardItemModel/QTableView/QItemDelegate in my app.
QComboBox is one of the columns and it is made shown for all the time using QTableView::openPersistentEditor.
i need:
1. when user clicks the QCombox and change the current index, i notify the new change to the model while the current index stays unchaged until the model is updated.
2. i want to get notified as soon as user changes the current index. while the problem is QItemDelegate::setModelData is never called until the QComboBox is focused out.
i tried this:
void
MyItemDelegate::createEditor(..)
{
QComboBox * comboEditor = new QComboBox(parent);
comboEditor->addItem("1");
comboEditor->addItem("2");
connect(comboEditor, SIGNAL(currentIndexChanged(int)), this, SLOT(emitDataCommit(int)));
return comboEditor;
}
void
MyItemDelegate::emitDataCommit(int)
{
emit commitData(qobject_cast<QWidget *>(sender()));
}
void
MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(comboBox);
QString newText = comboBox->currentText();
/// set it back to the previous until the model is updated using the new data.
/// and the current index is automatically updated by the model when it is finished updating.
if (newText == "1")
{
comboBox->setCurrentText("2");
}
else if (newText == "2")
{
comboBox->setCurrentText("1");
}
model->setData(index, newText);
}
BUT when the model accepts the new data and call setData() to update itself, the signal comboEditor::currentIndexChanged(int) is emitted for the second time by the model and MyItemDelegate::setModelData is called again.....
is there anyway to prevent the model from emiting the signal or is there anyway to distinguish the two call to MyItemDelegate::setModelData?
I use QStandardItemModel/QTableView/QItemDelegate in my app.
QComboBox is one of the columns and it is made shown for all the time using QTableView::openPersistentEditor.
i need:
1. when user clicks the QCombox and change the current index, i notify the new change to the model while the current index stays unchaged until the model is updated.
2. i want to get notified as soon as user changes the current index. while the problem is QItemDelegate::setModelData is never called until the QComboBox is focused out.
i tried this:
void
MyItemDelegate::createEditor(..)
{
QComboBox * comboEditor = new QComboBox(parent);
comboEditor->addItem("1");
comboEditor->addItem("2");
connect(comboEditor, SIGNAL(currentIndexChanged(int)), this, SLOT(emitDataCommit(int)));
return comboEditor;
}
void
MyItemDelegate::emitDataCommit(int)
{
emit commitData(qobject_cast<QWidget *>(sender()));
}
void
MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(comboBox);
QString newText = comboBox->currentText();
/// set it back to the previous until the model is updated using the new data.
/// and the current index is automatically updated by the model when it is finished updating.
if (newText == "1")
{
comboBox->setCurrentText("2");
}
else if (newText == "2")
{
comboBox->setCurrentText("1");
}
model->setData(index, newText);
}
BUT when the model accepts the new data and call setData() to update itself, the signal comboEditor::currentIndexChanged(int) is emitted for the second time by the model and MyItemDelegate::setModelData is called again.....
is there anyway to prevent the model from emiting the signal or is there anyway to distinguish the two call to MyItemDelegate::setModelData?