Reputation: 53
I have set up a QItemDelegate that has an editor which is a QComboBox. I have set this item delegate to my list view.
Currently, only when I click on an item in my list view does the combo box show for that particular item. I made it one click by doing this:
ui->suggestedListView->setEditTriggers( QAbstractItemView::AllEditTriggers );
What I want is, for every item in a list view show its combobox instead of making the user double click on it to be able to see it.
Here is my item delegate:
#include "include/gui/comboboxdelegate.h"
ComboBoxDelegate::ComboBoxDelegate( QObject *parent )
: QItemDelegate( parent )
{
}
QWidget *ComboBoxDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex & ) const
{
QComboBox *editor = new QComboBox( parent );
editor->addItem( "Address" );
editor->addItem( "Address2" );
editor->addItem( "City" );
editor->addItem( "Country" );
editor->addItem( "Date of Birth" );
editor->addItem( "Email" );
editor->addItem( "Fax Number" );
editor->addItem( "First Name" );
editor->addItem( "Gender" );
editor->addItem( "Last Activity Timestamp" );
editor->addItem( "Last Name" );
editor->addItem( "Middle Name" );
editor->addItem( "Mobile Number" );
editor->addItem( "Phone Number" );
editor->addItem( "Reference Code" );
editor->addItem( "Signup Category" );
editor->addItem( "IP Address" );
editor->addItem( "Signup Timestamp" );
editor->addItem( "Signup URL" );
editor->addItem( "State/Province/Region" );
editor->addItem( "Zip/Postal Code" );
editor->addItem( "Discard" );
return editor;
}
void ComboBoxDelegate::setEditorData( QWidget *editor,
const QModelIndex &index ) const
{
QString value = index.model()->data( index, Qt::EditRole ).toString();
QComboBox *cBox = static_cast<QComboBox *>( editor );
cBox->setCurrentIndex( cBox->findText( value ) );
}
void ComboBoxDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index ) const
{
QComboBox *cBox = static_cast<QComboBox *>( editor );
QString value = cBox->currentText();
model->setData( index, value, Qt::EditRole );
}
void ComboBoxDelegate::updateEditorGeometry( QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex & ) const
{
editor->setGeometry( option.rect );
}
And here is how I'm setting up my qlist view:
ui->suggestedListView->setItemDelegate( new ComboBoxDelegate( ui->suggestedListView ) );
ui->suggestedListView->setEditTriggers( QAbstractItemView::AllEditTriggers );
Is this even possible? If not, what could be another solution?
Upvotes: 1
Views: 401
Reputation: 8994
QStyle::drawControl
::editorEvent
) in next way: create an editor (QComboBox
) and force it to show dropdown list.P.S. use QStyledItemDelegate
instead of QItemDelegate
Upvotes: 2