setEditorData() for QComboBox Delegate
Hi!
I have been studying the Spin Box Delegate Example and I think I understood how it works.
Now I am trying to create the delegate for a qcombobox.
I have transformed all the SpinBox functions but I cannot do the same for the setEditorData().Any ideas?
Code:
#include <QtGui>
#include "delegate.h"
#include <delegate.h>
#include <comboboxdelegate.h>
ComboBoxDelegate
::ComboBoxDelegate(QObject *parent
){
}
{
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");
return editor;
}
void ComboBoxDelegate
::setEditorData(QWidget *editor,
{
// QString str = index.model()->data(index, Qt::EditRole);
// QComboBox *comboBox = static_cast<QComboBox*>(editor);
// comboBox->setValue(value);
// comboBox->setCurrentIndex(value);
}
{
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
//comboBox->interpretText();//is this important for the QComboBox delegate??
QString str
= comboBox
->currentText
();
model->setData(index, str, Qt::EditRole);
}
void ComboBoxDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
Re: setEditorData() for QComboBox Delegate
Code:
comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));
Re: setEditorData() for QComboBox Delegate
thanks!
can i ask a question?
the combobox appears only if I double-click the cell which has a combobox delegate.
Is is possible to have the combobox always in the cell (even if that cell is not clicked at all). I would like to use the mouse wheel to change its value.
Re: setEditorData() for QComboBox Delegate
It's technically possible (it's called a "persistent editor") but you probably don't want that as having too many of those significantly slows down the application. It's better to reimplement editorEvent() in the delegate and force edit mode when a wheel event for an item is detected.
Re: setEditorData() for QComboBox Delegate
Thanks wysota! It worked:
Code:
for ( int i = 0; i < model.rowCount(); ++i )
tableView.
openPersistentEditor( model.
index(i,
1,
QModelIndex()) );
When it starts though, the combobox show a blank value. I tried setCurrentIndex(1) but it is totally ignored:
Code:
{
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");
editor->setCurrentIndex(1);
return editor;
}
Re: setEditorData() for QComboBox Delegate
Set the index in setEditorData().
Re: setEditorData() for QComboBox Delegate
Do you mean this?
Code:
void ComboBoxDelegate
::setEditorData(QWidget *editor,
{
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
comboBox->setCurrentIndex( 1 );
// comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));
}
In this way the combo box will not show any changes that occur in the database..it will always show index 1.
Is there a way to do it in the createEditor function?
Re: setEditorData() for QComboBox Delegate
Uncomment line #6 in your code and change the findText() part to something that detects if the string in the model is present in the combobox before you blindly set it on the combo.
Re: setEditorData() for QComboBox Delegate
Quote:
Uncomment line #6 in your code and change the findText() part to something that detects if the string in the model is present in the combobox before you blindly set it on the combo.
I am sorry but I did not understand your correction.
The combo has three items:
Code:
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");
When the program starts running I would like the combo to show the first item "a" instead of blank.
Then the user can choose any item s/he wishes.
Is there a way to initialize the combo to item "a"?
Re: setEditorData() for QComboBox Delegate
The combobox has to show whatever data is in the model for the current cell and not what you want. You can say that if the model doesn't return data for an index, "a" should be set as the combobox current item. But then the model will never contain "a" (or its equivalent) unless you first change the combobox to something else, close the editor, reopen it again and change the cell back to "a".
Re: setEditorData() for QComboBox Delegate
I have been working on the same thing. Like wysota said, the combobox has to show whatever data is in the model for the current cell. to initialise the combobox to 'a', set the value in the model to 'a'. Then in the setEditorData() function you can retrieve the value in the model using something like this.
Code:
QString value
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
Then you can set the current index like you have previously.
Code:
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
comboBox->setCurrentIndex(comboBox->findText(value));