How to set QComboxItem in a QStyledItemDelegate
-
Could not get it to work. item->setData() and model->setData() for every role (UserRole,EditRole, etc) would never trigger a QStyledItemDelegate::setModelData();
Maybe it is a bug
-
Neither will, setModelData is called after you closed the editor
-
Yeah I know now. So do you have any advise on how I can achieve my goal here
-
You wrote: comboBox->setCurrentIndex(1);
Where does this "1" come from ? Is it a fixed value ?
-
Just forcing the index to be one so I can see something. The combobox has at least two entries
-
Subclass and reimplement QStyledItemDelegate::createEditor
call QStyledItemDelegate::createEditor version there then check
if widget returned from there is combobox, like below@QWidget* MyStyledItemDelegate::createEditor( QWidget parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QWidget w = QStyledItemDelegate::createEditor( parent, option, index);
if( w->inherits("QComboBox") )
{
QComboBox* edit = static_cast<QComboBox*>( w );
... do smth}
return w;
}
@
. -
Yes I have implemented createEditor(), it is used to create a combo box when a user clicks on a cell item, but how to call it from outside as you suggest is the question ? What do I use for the arguments. For instance what do I supply for the second argument -QStyeOptionViewItem
-
just do it in setEditorData
@
void MyDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
QComboBox * comboBox = qobject_cast<QComboBox*>(editor);
if (comboBox) {
comboBox.setCurrentIndex(1);
}
}
@Again, these functions are called for you, you don't call them
-
You do not call createEditor yourself, it will be called with appropriate parameters. All you need is replace base class version with yours and
add the behavior you want.Also MyDelegate::setEditorData mentioned above might be an option I think you goal was to change the behavior only once, so I think createEditor is better choice, but can't state it without testing.
-
Well I got it to work for one item, but for two or more items the second item fails. I think the call to edit gets on the event loop and so things are not whre they ought to be. Here is what I have
@// main loop where I want to set the data:
for (int i=i=0;i<childCount;i++) {
valueDestin = (FieldValueItem)destination.child(i,AvailableValueCol);
QModelIndex mi = valueDestin->index();
if ((i == 2) || (i == 3)) {
fieldValueDelegate->setSilent(true,1); // set index to item
availableFieldsTree->edit(mi);
// qApp->processEvents(QEventLoop::WaitForMoreEvents,500);
//availableFieldsTree->closePersistentEditor(mi);
//qApp->processEvents(QEventLoop::WaitForMoreEvents,500);
fieldValueDelegate->setSilent(false,-1);
}
....
void FieldValueDelegate::setSilent(bool on, int ci)
{
isSilent = on;
comboIndex = ci;
if (!isSilent)
editCB = 0;
}
void FieldValueDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
qDebug() << "Set Editor Data" << FILE << LINE;
QComboBox *cb = qobject_cast <QComboBox *> (editor);
if (cb && isSilent) {
cb->setCurrentIndex(comboIndex);
}
QStyledItemDelegate::setEditorData(editor,index);
}@
If I just have i set to 2, or i set to 3 (that is one index) in the loop above the index will get set, and I will see the result in my treeview, but when I try to both indexes in it fails on the second pass . There is an error message from Qt on the second iiteration:
editing failedI guess Qt was not made to do this kind of stuff. If you know a quick fix please let me know, otherwise I will just give this thread a rest. Thanks to all that tried to help