PDA

View Full Version : setEditorData() for QComboBox Delegate



fatecasino
28th February 2011, 18:04
Hi!

I have been studying the Spin Box Delegate Example (http://trinity.pearsoncomputing.net/docs/qt4/itemviews-spinboxdelegate.html) 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?


#include <QtGui>
#include "delegate.h"
#include <delegate.h>
#include <comboboxdelegate.h>

ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");
return editor;
}

void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// QString str = index.model()->data(index, Qt::EditRole);

// QComboBox *comboBox = static_cast<QComboBox*>(editor);
// comboBox->setValue(value);
// comboBox->setCurrentIndex(value);


}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
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,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

wysota
28th February 2011, 19:58
comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));

fatecasino
3rd March 2011, 21:45
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.

wysota
3rd March 2011, 21:59
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.

fatecasino
3rd March 2011, 23:37
Thanks wysota! It worked:


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:



QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");

editor->setCurrentIndex(1);
return editor;
}

wysota
4th March 2011, 01:05
Set the index in setEditorData().

fatecasino
4th March 2011, 01:39
Do you mean this?


void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
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?

wysota
4th March 2011, 10:32
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.

fatecasino
4th March 2011, 13:07
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:


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"?

wysota
4th March 2011, 15:54
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".

mickgraham
6th April 2011, 02:00
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.


QString value = index.model()->data(index, Qt::DisplayRole).toString();

Then you can set the current index like you have previously.


QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->setCurrentIndex(comboBox->findText(value));