void IPinput::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QLineEdit *edit = static_cast <QLineEdit*> (editor); QString strEdit = edit->text(); if (!correctIP(strEdit)){ model->setData(index,Qt::red,Qt::TextColorRole);//пока просто крашу некорректные данные } model->setData(index,strEdit,Qt::EditRole);}...bool IPinput::eventFilter(QObject *obj, QEvent *event){ QLineEdit *edit = static_cast<QLineEdit *> (obj); if (event->type() == QEvent::KeyPress) { QKeyEvent *keyPress = static_cast<QKeyEvent *>(event); if(keyPress->key() == Qt::Key_Escape){ edit->setText(oldInputIP); return true; }else return QItemDelegate::eventFilter(obj, event); if (event->type()== QEvent::FocusOut){ //тут можно вставить проверку, но , //что при true, что при false вызывается диструктор return false; } }else{ return QItemDelegate::eventFilter(obj, event); }}
void QAbstractItemView::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint )
class closeDelegate: public QAbstractItemDelegate{ void closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint);};....closeDelegate *Item = new closeDelegate();
C++ (Qt)...closeDelegate::closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint) {... if (expr) { ... //closing editor QAbstractItemDelegate::closeEditor(editor, hint); } else { ... //ignore return; }}...
class IPinput : public QItemDelegate//редактирование IP таблиц{ Q_OBJECTpublic: IPinput(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 closeEditor(QWidget *editor, EndEditHint hint);public slots: bool eventFilter(QObject *obj, QEvent *event); // bool eventFilter(QObject *obj, QEvent *event);protected : virtual void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);//сюда не попадаем};...void IPinput::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint){ return;}.... ui->tableSpisok->setItemDelegateForColumn(2,new IPinput());}
virtual void destroyEditor(QWidget * editor, const QModelIndex & index) const
class closeDelegate: public QAbstractItemView{ protected: void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);};...void closeDelegate::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint){ return;}
class IPinput : public QItemDelegate//редактирование IP таблиц{ Q_OBJECTpublic: IPinput(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 closeEditor(QWidget *editor, EndEditHint hint);public slots: bool eventFilter(QObject *obj, QEvent *event); // bool eventFilter(QObject *obj, QEvent *event);protected : // virtual void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);};....ui->tableSpisok->setItemDelegateForColumn(2,new IPinput());ui->tableSpisok->setItemDelegateForColumn(1,new IPinput());
C++ (Qt)class closeDelegate: public QTableWidget{ protected: void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);};