Custom QItemDelegate destroy editor on focus out
-
I have subclassed QItemDelegate in order to show a qspinbox and a numpad to insert numbers into the table:
@class HwIoBoardTableItemDelegate: public QItemDelegate
{
Q_OBJECTpublic:
HwIoBoardTableItemDelegate(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; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};@
And the implementation:
@QWidget *HwIoBoardTableItemDelegate::createEditor(QWidget parent,
const QStyleOptionViewItem &/ option */,
const QModelIndex & index ) const
{
QWidget * editor = 0;switch (index.column()) { case 1: editor = new myQSpinBoxDirect(parent); break; case 2: case 3: editor = new myQSpinBoxDirect(parent,0,32); break; case 4: editor = new QComboBox(parent); for(int i = 0; i<Params::Instance()->hwIoBoardType.count();i++){ ((QComboBox*)editor)->addItem(Params::Instance()->hwIoBoardType.at(i),Params::Instance()->hwIoBoardType.at(i)); } break; default: break; } return editor;
}
void HwIoBoardTableItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
myQSpinBoxDirect *spinBox;
QComboBox *combo;
int valueInt;
QString valueString;switch (index.column()) { case 1: case 2: case 3: valueInt = index.model()->data(index, Qt::EditRole).toInt(); spinBox = static_cast<myQSpinBoxDirect*>(editor); spinBox->setValue(valueInt); break; case 4: valueString = index.model()->data(index, Qt::EditRole).toString(); combo = static_cast<QComboBox*>(editor); for(int i = 0; i<Params::Instance()->hwIoBoardType.count();i++){ if(Params::Instance()->hwIoBoardType.at(i).compare(valueString)==0){ combo->setCurrentIndex(i); } } default: break; }
}
void HwIoBoardTableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
myQSpinBoxDirect *spinBox;
QComboBox *combo;
int valueInt;
QString valueString;switch (index.column()) { case 1: case 2: case 3: spinBox = static_cast<myQSpinBoxDirect*>(editor); valueInt = spinBox->value(); model->setData(index, valueInt, Qt::EditRole); break; case 4: combo = static_cast<QComboBox*>(editor); valueString = combo->itemData(combo->currentIndex()).toString(); model->setData(index, valueString, Qt::EditRole); default: break; }
}
void HwIoBoardTableItemDelegate::updateEditorGeometry(QWidget editor,
const QStyleOptionViewItem &option, const QModelIndex &/ index */) const
{
editor->setGeometry(option.rect);
}
@And the myQSpinBoxDirect extended QSpinbox in that way:
@class myQSpinBoxDirect: public QSpinBox
{
Q_OBJECTpublic:
myQSpinBoxDirect(QWidget *parent = 0,int minimum = 0, int maximum = 1000000);
~myQSpinBoxDirect();bool event(QEvent *event);
protected:
numPadLinked *num;
};@@myQSpinBoxDirect::myQSpinBoxDirect(QWidget * parent, int minimum, int maximum):QSpinBox(parent)
{
this->setButtonSymbols(QAbstractSpinBox::NoButtons);
this->setParent(parent);
this->setMinimum(minimum);
this->setMaximum(maximum);
this->show();num=new numPadLinked(0,false); connect(num,SIGNAL(value_changed_int(int)),this,SLOT(setValue(int))); connect(num,SIGNAL(closing()),num,SLOT(deleteLater())); connect(num,SIGNAL(ok()),num,SLOT(deleteLater())); num->show(""); num->raise(); num->activateWindow();
}
myQSpinBoxDirect::~myQSpinBoxDirect()
{
int i = 0;
}bool myQSpinBoxDirect::event(QEvent *event)
{if(QSpinBox::event(event)){ qDebug()<<event->type(); return true; }else{ return false; }
}
@The problem is when the numpad i showed the mySpinBoxDirect loose the focus(maybe) and is destroyed so I can't change the value by the numpad.
On windows platform it works fine in linux doesn't.
Any suggestions?