How to color a row of qSqlQueryModel where data of cell is smth
-
@SimonaBolgradova said in How to color a row of qSqlQueryModel where data of cell is smth:
and how to color the whole row.
I already answered this:
If you want all cells in a row to be drawn a certain way based on the content/value of one cell, you will have to get the other cells to look in that one cell in the row when it is their turn to be drawn in order to dictate how they themselves are to be drawn.
But you do not seem to have acknowledged this.
Note that if only the value in the Message column changes you want that to affect the color of all the other cells in that row. It is your responsibility to tell the view that those other cells have changed and need their color to be redrawn (else it will only do the Message column in the row), e.g. via a suitable
dataChanged()signal. Although @VRonin mentionssibling()in his code he does not seem to have pointed out that you will need to cause the redraw in those columns?@JonB said in How to color a row of qSqlQueryModel where data of cell is smth:
does not seem to have pointed out that you will need to cause the redraw in those columns?
Excellent spot as always!
connect(model,QAbstractItemModel::dataChanged,view,[=](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles){ if(!roles.isEmpty() && !roles.contains(Qt::DisplayRole)) return; if(ColumnWithAlarms < topLeft.column() || ColumnWithAlarms > bottomRight.column()) return; view->update(); }); -
You are overcomplicating things, it's super straightforward:
class ColorDelegate: public QStyledItemDelegate{ Q_OBJECT public: using QStyledItemDelegate::QStyledItemDelegate; const QSet<QString>& alarms() const {return m_alarms;} void setAlarms(const QSet<QString>& alm) {m_alarms=alm;} protected: void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override{ QStyledItemDelegate::initStyleOption(option,index); if(m_alarms.contains(index.data().toString()) option->backgroundBrush = QBrush(Qt::yellow, Qt::SolidPattern); } private: QSet<QString> m_alarms; };how to color the whole row.
Instead of
index.data().toString()useindex.sibling(index.row(),ColumnWithAlarms).data().toString()P.S.
Thedata()solution is perfectly valid, I disagree with it only on a philosophical level (philosophy has hardly any place in programming anyway) as the color isn't data, it's a way to paint existing data. At the end of the day, use the solution you find easier@VRonin how to get alarms from my slot
private slots: void alarms(QSet<QString> set){ setAlarms(set); }Is it smth like that?
and why I have this error

-
@VRonin how to get alarms from my slot
private slots: void alarms(QSet<QString> set){ setAlarms(set); }Is it smth like that?
and why I have this error

@SimonaBolgradova said in How to color a row of qSqlQueryModel where data of cell is smth:
how to get alarms from my slot
Don't know what this means.
and why I have this error
Start with how can the method be
constif it altersm_alarms? [ @VRonin ?? :) ] -
@SimonaBolgradova said in How to color a row of qSqlQueryModel where data of cell is smth:
how to get alarms from my slot
Don't know what this means.
and why I have this error
Start with how can the method be
constif it altersm_alarms? [ @VRonin ?? :) ]@JonB said in How to color a row of qSqlQueryModel where data of cell is smth:
Start with how can the method be const if it alters m_alarms? [ @VRonin ?? :) ]
Yep, corrected.
P.S.
In most use cases you don't need a slot. Using Qt5 connections you can connect directly to thesetAlarmsmethod -
m_alarms keeps to be empty :(
I connected like that in another class named Controllerconnect( s, &SQLQueries::alarms, d, &ColorDelegate::setAlarms );Alarms signal will be emitted when I triggered action on and m_alarms get some values but then in initStyleOption m_alarms is empty.

-
void MainWindow::setTable(QSqlQueryModel * model) { ui->tableView->setModel( model ); ui->tableView->setItemDelegate(new ColorDelegate(ui->tableView)); ui->tableView->resizeColumnsToContents(); } -
void MainWindow::setTable(QSqlQueryModel * model) { ui->tableView->setModel( model ); ui->tableView->setItemDelegate(new ColorDelegate(ui->tableView)); ui->tableView->resizeColumnsToContents(); }@SimonaBolgradova said in How to color a row of qSqlQueryModel where data of cell is smth:
ui->tableView->setItemDelegate(new ColorDelegate(ui->tableView));
Fire and forget pointer. The delegate you are connecting is not the one used by the view then. what is
dind, &ColorDelegate::setAlarms?It should be something like:
auto alarmDelegate = new ColorDelegate(ui->tableView); ui->tableView->setItemDelegate(alarmDelegate); connect(s, &SQLQueries::alarms, alarmDelegate, &ColorDelegate::setAlarms); -
@SimonaBolgradova said in How to color a row of qSqlQueryModel where data of cell is smth:
ui->tableView->setItemDelegate(new ColorDelegate(ui->tableView));
Fire and forget pointer. The delegate you are connecting is not the one used by the view then. what is
dind, &ColorDelegate::setAlarms?It should be something like:
auto alarmDelegate = new ColorDelegate(ui->tableView); ui->tableView->setItemDelegate(alarmDelegate); connect(s, &SQLQueries::alarms, alarmDelegate, &ColorDelegate::setAlarms);Fire and forget pointer. The delegate you are connecting is not the one used by the view then. what is
dind, &ColorDelegate::setAlarms?in controller.h
private: ColorDelegate * d = new ColorDelegate();It should be something like:
auto alarmDelegate = new ColorDelegate(ui->tableView); ui->tableView->setItemDelegate(alarmDelegate); connect(s, &SQLQueries::alarms, alarmDelegate, &ColorDelegate::setAlarms);this doesn't work and all fields are empty in my QTableView
-
Fire and forget pointer. The delegate you are connecting is not the one used by the view then. what is
dind, &ColorDelegate::setAlarms?in controller.h
private: ColorDelegate * d = new ColorDelegate();It should be something like:
auto alarmDelegate = new ColorDelegate(ui->tableView); ui->tableView->setItemDelegate(alarmDelegate); connect(s, &SQLQueries::alarms, alarmDelegate, &ColorDelegate::setAlarms);this doesn't work and all fields are empty in my QTableView
@SimonaBolgradova maybe the problem is that my connection was created in the Controller class. There I create an instance of ColorDelegate and m_alarms is still empty. But how could I connect two classes into another without creating an instance?
-
What you are doing at the moment is
int a=0; int b=3;and asking whyais 0 and not 3. They are the same type (int) but 2 different objects. You need to callsetAlarmson the same object as the one used in the view.But how could I connect two classes into another without creating an instance?
This question is asked weekly if not daily in this forum just search for "connecting to class". The short answer is that you should structure your app as a tree where the leaves are connected by the branches
-
What you are doing at the moment is
int a=0; int b=3;and asking whyais 0 and not 3. They are the same type (int) but 2 different objects. You need to callsetAlarmson the same object as the one used in the view.But how could I connect two classes into another without creating an instance?
This question is asked weekly if not daily in this forum just search for "connecting to class". The short answer is that you should structure your app as a tree where the leaves are connected by the branches
@VRonin
Thank you very much for your time! Now the code is working properly. 😊