Qtableview set background color to some columns
-
@n-2204 said in Qtableview set background color to some columns:
I need to enter integer values in column
You don't need to do anything special. The default delegate already does this.
If, and this is a guess on my part, you are usingQStandardItemModel
you can solve your problem by never using the constructorsQStandardItem(QIcon,QString)
orQStandardItem(QString)
. Just construct an empty item and then callitem->setData
and pass yor integer to it, do not convert it to a string. You'll see the default delegate (or the modifed version I posted above) will do exactly what you want -
Disclaimer
I think your delegate is doing more harm than good but i'll go along with it anyway.
Inside the class you defined in intvalmodel.h add:
public: void setBrushForColumn(int col, const QBrush& brush); protected: void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override; private: QMap<int,QBrush> m_brushes;
now intvalmodel.cpp
#include "intvalmodel.h" #include "GAS_tool.h" intvalmodel::intvalmodel(QObject* parent) :QStyledItemDelegate(parent) { } QWidget* QStyledItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QLineEdit* editor = new QLineEdit(parent); // QIntValidator* data = new QIntValidator(0, 999); //editor->setValidator(data); editor->setValidator(new QIntValidator); return editor; } void intvalmodel::setEditorData(QWidget* editor, const QModelIndex& index) const { QString value1 = index.model()->data(index, Qt::EditRole).toString(); QLineEdit* line = static_cast<QLineEdit*>(editor); line->setText(value1); } void intvalmodel::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QLineEdit* line = static_cast<QLineEdit*>(editor); model->setData(index, line->text().toInt()); } void intvalmodel::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { editor->setGeometry(option.rect); } void intvalmodel::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override { QStyledItemDelegate::initStyleOption(option,index); option->backgroundBrush = m_brushes.value(index.column(),option->backgroundBrush); } void intvalmodel::setBrushForColumn(int col, const QBrush& brush){ if(col<0 || col >= columnCount()) return; m_brushes[col]=brush; }
Now you can use it:
delegate = new intvalmodel(); delegate ->setBrushForColumn(1,QBrush(Qt::yellow)); delegate ->setBrushForColumn(2,QBrush(Qt::red)); delegate ->setBrushForColumn(3,QBrush(Qt::blue)); ui.tableView->setItemDelegateForColumn(1,delegate); ui.tableView->setItemDelegateForColumn(2, delegate); ui.tableView->setItemDelegateForColumn(3,delegate);
-
class BackgroundBrushDelegate : public QStyledItemDelegate{ Q_OBJECT Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush NOTIFY backgroundBrushChanged) Q_DISABLE_COPY(BackgroundBrushDelegate) public: explicit BackgroundBrushDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {} BackgroundBrushDelegate(const QBrush& brush, QObject *parent = nullptr) : QStyledItemDelegate(parent) , m_backgroundBrush(brush) {} const QBrush& backgroundBrush() const { return m_backgroundBrush; } void setBackgroundBrush(const QBrush& brush) { if(m_backgroundBrush==brush) return; m_backgroundBrush=brush; backgroundBrushChanged(m_backgroundBrush); } signals: void backgroundBrushChanged(const QBrush& brush); protected: void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override { QStyledItemDelegate::initStyleOption(option,index); option->backgroundBrush = m_backgroundBrush; } private: QBrush m_backgroundBrush; }
when i am using this code
after applying brushdelegate in that column i am not able to type other then integer can you plz help me how to do ? -
@n-2204 said in Qtableview set background color to some columns:
after applying brushdelegate in that column i am not able to type other then integer can you plz help me how to do ?
This does not sound like a brush delegate issue. Earlier you have in
createEditor()
:editor->setValidator(new QIntValidator);
Make sure that is applying to the column (look at
index
parameter) where you want an integer, not some other column, at a guess? -
@n-2204 said in Qtableview set background color to some columns:
stuck for some assignment
I'll quote the wise @mrjj
the teacher will catch it and fail them if too much code is just copied.
We gave you 2 possible solutions, one with and one without validator. We even integrated it in your existing code. What more can we do to help?
-
@VRonin Hi! I am trying to do something similar; however, I am building an application in python, not C++. Could you help me how to understand this code better so I can convert it to python? Or could you maybe show me how it would be in python?