Solved QItemDelegate
-
Hello friends, I have this code, I managed to put a QSpinBox in the first column of my QTableView, my question is how can I
do to put a certain color to the value, or to the bottom of the cell according to the value that is placed, for example if it is greater than
100 is painted red, any suggestion would be appreciated.Code:
#ifndef SPINBOXDELEGATE_H #define SPINBOXDELEGATE_H #include <QObject> #include <QItemDelegate> #include <QSpinBox> class SpinBoxDelegate : public QItemDelegate { Q_OBJECT public: explicit SpinBoxDelegate(QObject *parent = nullptr); // QAbstractItemDelegate interface public: virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override; virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; }; #endif // SPINBOXDELEGATE_H #include "spinboxdelegate.h" SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : QItemDelegate(parent) { } QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QSpinBox *sbControl=new QSpinBox(parent); sbControl->setMinimum(INT_MIN); sbControl->setMaximum(INT_MAX); (void)option; (void)index; return sbControl; } void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QSpinBox *sbControl=static_cast<QSpinBox *>(editor); int values=index.data().toInt(); sbControl->setValue(values); } void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QSpinBox *sbControl=static_cast<QSpinBox *>(editor); model->setData(index,sbControl->value(),Qt::EditRole); } void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); (void)index; } Main form: #ifndef QITEMDELEGATEDIALOG_H #define QITEMDELEGATEDIALOG_H #include <QDialog> #include "spinboxdelegate.h" #include <QStandardItemModel> namespace Ui { class QItemDelegateDialog; } class QItemDelegateDialog : public QDialog { Q_OBJECT public: explicit QItemDelegateDialog(QWidget *parent = nullptr); ~QItemDelegateDialog(); private: Ui::QItemDelegateDialog *ui; SpinBoxDelegate *spDelegate; QStandardItemModel *model; }; #endif // QITEMDELEGATEDIALOG_H #include "qitemdelegatedialog.h" #include "ui_qitemdelegatedialog.h" QItemDelegateDialog::QItemDelegateDialog(QWidget *parent) : QDialog(parent), ui(new Ui::QItemDelegateDialog) { ui->setupUi(this); spDelegate=new SpinBoxDelegate(this); model=new QStandardItemModel(5,3,this); ui->tableView->setModel(model); ui->tableView->setItemDelegateForColumn(0,spDelegate); } QItemDelegateDialog::~QItemDelegateDialog() { delete ui; }
-
@Bonnie thanks my friend, it is what I was looking for, solve my problem, greetings and success.
-
Hi,
You can use a QIdentityProxyModel and in the data method return the color you want for the ForegroundRole.
-
ok thank you, but i'm not sure about using that class.
-
Not sure about what ?
-
I mean how to apply that class, I don't know where to start, a practical example would help me, if possible of course. thanks
-
You could subclass QStandardItemModel like this:
#ifndef SPINBOXITEMMODEL_H #define SPINBOXITEMMODEL_H #include <QStandardItemModel> class SpinBoxItemModel : public QStandardItemModel { Q_OBJECT public: explicit SpinBoxItemModel(QObject *parent = nullptr); SpinBoxItemModel(int rows, int columns, QObject *parent = nullptr); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; }; #endif // SPINBOXITEMMODEL_H
#include "spinboxitemmodel.h" SpinBoxItemModel::SpinBoxItemModel(QObject *parent) : QStandardItemModel(parent) { } SpinBoxItemModel::SpinBoxItemModel(int rows, int columns, QObject *parent) : QStandardItemModel(rows, columns, parent) { } QVariant SpinBoxItemModel::data(const QModelIndex &index, int role) const { if(role == Qt::BackgroundRole && index.data().toInt() > 100) { return QBrush(Qt::red); } return QStandardItemModel::data(index, role); }
model=new SpinBoxItemModel(5,3,this);
Please note that after you just finish editing a spinbox, the background color of the cell is a "Highlighted" color (maybe blue). It might not be red even the value is greater than 100. You need to select another cell to see the actual background color.
-
@Bonnie thanks my friend, it is what I was looking for, solve my problem, greetings and success.
-
@lincoln said in QItemDelegate:
I mean how to apply that class, I don't know where to start, a practical example would help me, if possible of course. thanks
There's an example in the documentation of the class that I linked to.
-
@SGaist ok, thanks