Unsolved where to emit closeEditor() when subclass from QItemDelegate ?
-
Hi guys.
I have a tableview , one column need to set to combobox, so i subclass QItemDelegate. it works, except it cannot emit closeEditor() signal, I invoke emit closeEditor in setModelData(), but compiler says:error: C2662: “void QAbstractItemDelegate::closeEditor(QWidget *,QAbstractItemDelegate::EndEditHint)”: 不能将“this”指针从“const ComboboxDelegate”转换为“QAbstractItemDelegate &”
here is the code:
class ComboboxDelegate: public QItemDelegate { public: ComboboxDelegate(QWidget *parent = nullptr,QStringList items=QStringList(),int currentIndex=0):QItemDelegate(parent) { m_items = items; m_initIdx = currentIndex; } QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override //final { Q_UNUSED(parent) Q_UNUSED(option) Q_UNUSED(index) QComboBox *channelBox = new QComboBox(parent); channelBox->setFixedHeight(option.rect.height()); for(int i=0;i<m_items.size();i++){ QString item = m_items.at(i); channelBox->addItem(item); } channelBox->setCurrentIndex(m_initIdx); return channelBox; } void setEditorData(QWidget *editor, const QModelIndex &index) const { QComboBox *comboBox = static_cast<QComboBox*>(editor); if(comboBox){ QString str = index.model()->data(index, Qt::EditRole).toString(); comboBox->setCurrentIndex(comboBox->findText(str)); } } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *comboBox = static_cast<QComboBox*>(editor); if(comboBox!=0){ model->setData(index, comboBox->currentText(), Qt::EditRole); emit closeEditor(editor); // ERROR HERE ~~~~ } } void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } // still cannot work with my signal ~~~ signals: void closeEditor2(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint); private: int m_initIdx; QStringList m_items; };
Any help will be appreciated.
-
You don't have to emit this signal, it's emitted by QAbstractItemDelegate to notify the view.
-
@QtTester
As @Christian-Ehrlicher has said. Your code will be fine without anyemit
s here.But (quite separate issue) you might want to change your
static_cast<QComboBox*>
s toqobject_cast<QComboBox*>
s. It's safer, and allows you to use the delegate against different columns/widget types without having to invent separate delegates for each one. Depending on how you like to do these things. -
@Christian-Ehrlicher said in where to emit closeEditor() when subclass from QItemDelegate ?:
You don't have to emit this signal, it's emitted by QAbstractItemDelegate to notify the view.
@JonB said in where to emit closeEditor() when subclass from QItemDelegate ?:
@QtTester
As @Christian-Ehrlicher has said. Your code will be fine without anyemit
s here.But (quite separate issue) you might want to change your
static_cast<QComboBox*>
s toqobject_cast<QComboBox*>
s. It's safer, and allows you to use the delegate against different columns/widget types without having to invent separate delegates for each one. Depending on how you like to do these things.Thanks you both. But I didnot recevie the signal, after I connect the abstractitemdelegate signal, when I finish edit the default lineedit cell ,it emits a signal ,but after I choose the combobox , the signal did not send out!
connect(ui->tableViewSpdLimit->itemDelegate(),&QAbstractItemDelegate::closeEditor,this,[=](){ qDebug("detect edit!"); ui->pushButtonSubmitLimit->setEnabled(true); });
-
Already solved, use ui->tableViewSpdLimit->itemDelegateForColumn()