#ifndef DELEGATEVVODA_H#define DELEGATEVVODA_H#include <QItemDelegate>class delegatevvoda : public QItemDelegate{ Q_OBJECTpublic: explicit delegatevvoda(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: signals: public slots: };#endif // DELEGATEVVODA_H
#include "delegatevvoda.h"#include "QLineEdit"delegatevvoda::delegatevvoda(QObject *parent) : QItemDelegate(parent){}QWidget *delegatevvoda::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const{ QLineEdit *LineEdit = new QLineEdit(parent); LineEdit->setMaxLength(50); return LineEdit;}void delegatevvoda:: 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 delegatevvoda:: setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QLineEdit *LineEdit = qobject_cast<QLineEdit *>(editor); model->setData(index,LineEdit->text()); return;}
QString v = "QTableView::item::focus{\n"\ "border: yellow;\n"\ "background-color :yellow;\n"\ "selection-color : blue;\n"\ "}" "QTableView {\n"\ "selection-color: black;\n"\ "selection-background-color: #7FFF00;\n"\ "}"; ui->tableView->setStyleSheet(v);
void delegatevvoda::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const{ editor->setGeometry(option.rect);}
LineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
class delegatevvoda : public QStyledItemDelegate{ Q_OBJECTpublic: explicit delegatevvoda(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();private: QLineEdit *LineEdit;};
delegatevvoda::delegatevvoda(QObject *parent) : QStyledItemDelegate(parent){}QWidget *delegatevvoda::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const{ QLineEdit *LineEdit = new QLineEdit(parent); connect(LineEdit, SIGNAL(textChanged(QString)), this, SLOT(resizeLineEditToContents())); LineEdit->setMaxLength(50); return LineEdit;}void delegatevvoda::resizeLineEditToContents(){ QString text = LineEdit->text(); QFontMetrics fm = LineEdit->fontMetrics(); int w = fm.boundingRect(text).width(); LineEdit->resize(w, LineEdit->height());}void delegatevvoda:: 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 delegatevvoda:: setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QLineEdit *LineEdit = qobject_cast<QLineEdit *>(editor); model->setData(index,LineEdit->text()); return;}