QComboBox does not call delegation methods

I want to customize QComboBox by inserting QWidgets (instead of strings) through the model and delegate:

QComboBox *cb = new QComboBox(this); FeatureModel *featureModel = new FeatureModel(cb); cb->setModel(featureModel); ComboBoxItemDelegate *comboBoxItemDelegate = new ComboBoxItemDelegate(cb); cb->setItemDelegate(comboBoxItemDelegate); 

FeatureModel inherits from QAbstractListModel and ComboBoxItemDelegate inherits from QStyledItemDelegate.

The problem is that delegate methods are never called, so my custom widget is not inserted (I only see FeatureModel lines). However, if I use QTableView instead of QComboBox, it works as it should.

Does anyone know where the error is? Am I missing some important aspect of the QT Model / View concept?

I appreciate every help.

Regards, Magnus

EDIT: Here is my delegate. With the exception of the constructor (of course), none of the following methods are called (there is no console output).

 ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent) : QStyledItemDelegate(parent) { qDebug() << "Constructor ComboBoxItemDelegate"; } ComboBoxItemDelegate::~ComboBoxItemDelegate() { } QWidget* ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const { qDebug() << "createEditor"; // never called QWidget *widget = new QWidget(parent); Ui::ComboBoxItem cbi; cbi.setupUi(widget); cbi.label->setText(index.data().toString()); widget->show(); return widget; } void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const { qDebug() << "setEditorData"; // never called } void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const { qDebug() << "setModelData"; // never called } 
+4
source share
1 answer

I think I found the problem.

First, make sure that viewing in QComboBox allows you to edit:

 cb->view()->setEditTriggers(QAbstractItemView::AllEditTriggers); 

I'm not sure this is a good practice, but it was the only way to get me to work. The default value for "editTriggers" is QAbstractItemView::NoEditTriggers

Secondly, make sure your model allows you to edit:

 Qt::ItemFlags MyModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::ItemIsEnabled; return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; } bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { // Modify data.. emit dataChanged(index, index); return true; } return false; } 

He has a problem. The first time you see a ComboBox, you can change the current text of the element, and it will not call the delegate methods for editing. You need to select one item and you can edit it.

In any case, I believe that using QComboBox for editable elements is inconsistent. Are you sure you need a QComboBox for this task?

Hope this helps

+2
source

Source: https://habr.com/ru/post/1446637/


All Articles