class delegatenynn : public QStyledItemDelegate{ Q_OBJECTpublic: explicit delegatenynn(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;private slots: void resizeLineEditToContents(); void toUpper(const QString &text);private: mutable QLineEdit *m_plineEdit; mutable QRect m_defaultRect;};
delegatenynn::delegatenynn(QObject *parent) : QStyledItemDelegate(parent){}QWidget *delegatenynn::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &) const{ QLineEdit *lineEdit = new QLineEdit(parent); QCompleter *productsCompleter=new QCompleter(); QSqlQueryModel *sqlProductsQuery=new QSqlQueryModel(productsCompleter); sqlProductsQuery->setQuery("SELECT DISTINCT ncolumn FROM table order by id desc LIMIT 500"); productsCompleter->setModel(sqlProductsQuery); lineEdit->setCompleter(productsCompleter); m_plineEdit = lineEdit; m_defaultRect = option.rect; m_plineEdit->text().toUpper(); connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(resizeLineEditToContents())); connect(lineEdit, SIGNAL(textEdited(const QString &)), SLOT(toUpper(const QString &))); lineEdit->setMaxLength(45); return lineEdit;}void delegatenynn::resizeLineEditToContents(){ QString text = m_plineEdit->text(); if(text != "") { QFontMetrics fm = m_plineEdit->fontMetrics(); int w = fm.boundingRect(text).width() + fm.maxWidth(); if(w >= m_defaultRect.width()) { m_plineEdit->resize(w, m_plineEdit->height()); } }}void delegatenynn::toUpper(const QString &text){ QLineEdit *le = qobject_cast<QLineEdit *>(sender()); if (!le) return; le->setText(text.toUpper());}void delegatenynn:: setEditorData(QWidget *editor, const QModelIndex &index) const{ QString value = index.model()->data(index, Qt::EditRole).toString(); QLineEdit *LineEdit = qobject_cast<QLineEdit *>(editor); LineEdit->setText(value);}void delegatenynn:: setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QLineEdit *LineEdit = qobject_cast<QLineEdit *>(editor); QString text = LineEdit->text(); model->setData(index,text); return;}
QWidget *delegatenynn::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &) const