PDA

View Full Version : How to get index of a combobox delegate selection



vieraci
20th July 2009, 15:17
In a delegate that uses a QComboBox as the editor, how does one retrieve the index of the selected item ?
I can only get the selected text from the item.

QTableWidgetItem* item = tableWidget->item(row, col);
qDebug() << "data is: " << item->data(0);

wysota
20th July 2009, 15:53
Use QComboBox API to ask for an index of an item with a given text.

spirit
20th July 2009, 15:56
maybe this exapmle QTDIR/examples/widgets/cons/imagedelegate.cpp will help you.

vieraci
21st July 2009, 00:06
Use QComboBox API to ask for an index of an item with a given text.

This doesn't make sense.
QComboBox::findText() is what I want, but QComboBox has no idea what is inside the combobox held in the delegate class, and the delegate class doesn't have any methods to get data.

wysota
21st July 2009, 00:45
Could you explain what you want to achieve?

aamer4yu
21st July 2009, 05:26
Wont QComboBox::currentIndex or QComboBox::currentText help you ??

vieraci
21st July 2009, 10:15
My delegate class:

class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
int indexOf(QString textToFind);
QSqlQueryModel *items;
};


Initialise the delegate:

sqlClause = "SELECT cat_text, return_value FROM phone_codes"
" order by return_value";
phone_type_delegate->items->setQuery(sqlClause);
m_ui->phoneTableWidget->setItemDelegateForColumn(0,phone_type_delegate);


Method to get selection:

void ClientDetailsDialog::onPhoneTableWidgetItemChanged (QTableWidgetItem* item)
{
QTableWidgetItem* item1 = m_ui->phoneTableWidget->item(item->row(), 0);
}


I can get the combo box text from the above code, but I need to retrieve the index of the combobox selection.

I can do it like this:


int ComboBoxDelegate::indexOf(QString textToFind)
{
int retVal = -1;
for (int x = 0; x < items->rowCount(); ++x)
{
if (items->data(items->index(x, 0)).toString() == textToFind)
{
retVal = x;
break;
}
}
return retVal;
}


Unless there's a simpler way of retrieving the index of the combo box, I'll have to settle for this solution. Nothing wrong with it, just figure there must be some method that simply gives the index.

aamer4yu
21st July 2009, 10:52
Did you come across QComboBox::findText or QComboBox::findData ??:rolleyes:

spirit
21st July 2009, 11:06
come on man, I've already posted an example, it has all what you want


...QTDIR/examples/widgets/cons/imagedelegate.cpp...

wysota
21st July 2009, 12:23
How do your setEditorData() and setModelData() implementations look like?

vieraci
21st July 2009, 14:44
come on man, I've already posted an example, it has all what you want

I don't see it...am I retarded or something ???

The only data methods QItemDelegate has is:

setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
setEditorData(QWidget *editor, const QModelIndex &index)

These are all SET something and the combobox in each method is a parameter. It doesn't address my solution. Where's the real combobox ? from what I've read, it doesn't really exist, only when you begin to edit the item it's created.

So, there IS no method to return the combo box index...because there is no real combo box ! (have I answered my own post here ?)

I wrote a method in my previous post that gets the index via the QSqlQueryModel member I added to my custom delegate class, as I said, it works, but is there another not-so-obvious method that doesn't require a hammer ?

vieraci
21st July 2009, 14:56
How do your setEditorData() and setModelData() implementations look like?


void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox *edit = qobject_cast<QComboBox *>(editor);
QString value = index.model()->data(index, Qt::EditRole).toString();
edit->setCurrentIndex(edit->findText(value));
}



void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *edit = qobject_cast<QComboBox *>(editor);
QString oldVal = index.model()->data(index, Qt::EditRole).toString();
QString newVal = edit->currentText();
if (oldVal != newVal)
{
model->setData(index, newVal);
}
// qDebug() << "setModelData: index = " << edit->currentIndex();
}


I know what you're going to say right here, the answer is in setModelData. sure, yes.
But I don't use QComboBox::currentIndex() here, I need to access it outside these methods to evaluate the selections/entry of both columns before calling a saveRecord() method.

wysota
21st July 2009, 16:37
So save it as a custom role of your model from within setModelData(). At this point you have to either accept the change or reject it. The combobox will be destroyed after that so you can't access it.