Editable QComboBox with QItemDelegate
Hi all :)
I am trying to customize my QTreeView to get an editable QComboBox into a column.
There are some example on the Net, but I haven't seen any with an editable QComboBox, and mine does not work properly...
Let me explain : if I click into the cell and select an item from the list it works : the selection is saved. But if I enter free text and change the focus, then the text is lost, and my DisplayRole role contains nothing. But if I enter some text again, then it will be saved...
I searched without success some hours but didn't find any clue, so I'm posting here, maybe someone has a good idea about this...
Now, some code :
Code:
{
comboBox->setEditable(true);
comboBox
->setInsertPolicy
(QComboBox::NoInsert);
comboBox->addItems(index.model()->data(index, CustomRoles::ValueRole).toStringList());
connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));
connect(comboBox->lineEdit(), SIGNAL(editingFinished()), this, SLOT(emitCommitData()));
QString currentText
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
int selectedItem = comboBox->findText(currentText);
if(selectedItem == -1)
comboBox->setEditText(index.model()->data(index, Qt::DisplayRole).toString());
else
comboBox->setCurrentIndex(selectedItem);
return comboBox;
}
Code:
{
QComboBox *comboBox
= qobject_cast<QComboBox
*>
(editor
);
comboBox->clear();
comboBox->addItems(index.model()->data(index, CustomRoles::ValueRole).toStringList());
QString currentText
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
int selectedItem = comboBox->findText(currentText);
if(selectedItem == -1)
comboBox->setEditText(index.model()->data(index, Qt::DisplayRole).toString());
else
comboBox->setCurrentIndex(selectedItem);
}
Code:
{
QComboBox* comboBox
= qobject_cast<QComboBox
*>
(editor
);
for(int i=0;i<comboBox->count();++i)
value.append(comboBox->itemText(i));
int selectedItem = comboBox->findText(comboBox->currentText());
if(selectedItem == -1)
{
model->setData(index, -1, CustomRoles::SelectedItemRole);
model->setData(index, comboBox->currentText(), Qt::DisplayRole);
}
else
{
model->setData(index, selectedItem, CustomRoles::SelectedItemRole);
model->setData(index, comboBox->itemText(selectedItem), Qt::DisplayRole);
}
model->setData(index, value, CustomRoles::ValueRole);
}
Code:
void ActionDelegate::emitCommitData()
{
emit commitData(qobject_cast<QWidget *>(sender()));
}
CustomRoles::SelectedItemRole represents the currently selected item, or -1 if it's free text
CustomRoles::ValueRole represents all the items of the combo box
Re: Editable QComboBox with QItemDelegate
You have to play a bit with event filters, so that for example when you focus out of the editor or when you press the enter key, the contents is saved into the model.
Re: Editable QComboBox with QItemDelegate
But why does
Code:
connect(comboBox->lineEdit(), SIGNAL(editingFinished()), this, SLOT(emitCommitData()));
not save the data from the QLineEdit ?
I also noticed that setEditorData is called twice after an edition...
Re: Editable QComboBox with QItemDelegate
What is emitCommitData()? Is it a slot? The line edit is not the editor widget, but part of it, so your emitCommitData() doesn't make much sense.
Re: Editable QComboBox with QItemDelegate
Well, as I posted on the first post it's a slot which calls
Code:
emit commitData(qobject_cast<QWidget *>(sender()));
EDIT : Yes, it's a slot :)
Re: Editable QComboBox with QItemDelegate
Yeah, but it doesn't make much sense, as the line edit is not the editor.
Re: Editable QComboBox with QItemDelegate
Huh, that is true :)
Ok, I will look how to use the event filter.
Re: Editable QComboBox with QItemDelegate
It is probably a noobish question, but how do I installEventFilter within createEditor, because since createEditor is const, calling myLineEdit->installEventFilter(this) produces an error...
I doesn't know why it is so complicated to put a simple QComboBox into a cell :confused:
Anyway, I tested with having persistent editors : it seems to work, but the method of showing an editor when needed is much better...
Re: Editable QComboBox with QItemDelegate
You have to cheat using const_cast(). You can take a look at the implementation of QItemDelegate and the factory class it uses to see how it is internally implemented. With all the event filters and stuff.
Re: Editable QComboBox with QItemDelegate
I will look at it, thanks :)
Re: Editable QComboBox with QItemDelegate
Excuse me, may be it's easy, but what is CustomRoles?
Re: Editable QComboBox with QItemDelegate
Quote:
Originally Posted by
faraslacks
Excuse me, may be it's easy, but what is CustomRoles?
[WIKI=QAbstractItemModel#Custom_item_roles]Custom item roles in ItemViews[/WIKI]