Unwanted checkbox in QTableView
-
Good afternoon,
I just have the weirdest problem I ever encountered.
I am running macOS, with Qt 15.15.2 clang x64, QtCreator latest stable, all from online installer.So - one of my projects, MDI application, has logger as a feature. MDI window's widget is QWidget on it I have QTableView for logs and QLineEdit and QComboBox for filtering.
Data source is QSqlTableModel + QSortFilterProxyModel - it works flawlessly. Database connection and log write from other MDI sub windows are all ok, read in this view is also ok.
Second column is int with id of the service that wrote the log entry.The annoying part: with QSqlTableModel+QSortFilterProxyModel the data is displayed properly.
But I wanted to change int to string with the name of the service, so I wrote quick class based on QSqlTableModel:
Header file:#ifndef LOGMODEL_H #define LOGMODEL_H #include <QSqlTableModel> #include <QObject> const QStringList services={"System","and","other","services","used"}; class LogModel : public QSqlTableModel { Q_OBJECT public: LogModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; }; #endif // LOGMODEL_H
Code:
#include "logmodel.h" LogModel::LogModel(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent,db) {} QVariant LogModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (index.column()==1) { int val = QSqlTableModel::data(index,role).toInt(); if (val>-1&&val<5) { return services.at(val); } else { return "Unknown service: "+QString::number(val); } } return QSqlTableModel::data(index,role); }
When I replaced QSqlTableModel with this model all of a sudden the altered column has a CHECKBOX on the left side of the text:
The view is genuine, with select single line options and I didn't touch the delegates. Edit triggers disabled.
Does any of you have an idea what happened? It's not the first time I use such simple class to alter data returned but the very first time I have something like that. -
@artwaw said in Unwanted checkbox in QTableView:
Does any of you have an idea what happened?
You don't respect the role in
LogModel::data()
-
@artwaw
probably the QSqlTableModel's implementation of flags() containsQt::ItemIsUserCheckable
for that column? -
@raven-worx said in Unwanted checkbox in QTableView:
probably the QSqlTableModel's implementation of flags() contains Qt::ItemIsUserCheckable for that column?
No, this only tells the view if the user can check or not. The drawing is done via data()
-
@Christian-Ehrlicher OMG, yes, I forgot. This is one of the most embarrassing mistakes I made.
Changed condition to
index.column()==1&&role==Qt::DisplayRole
and now it works, of course.Thank you, I am going to sit in the corner now.
-
The first check I write in my custom model's data() function is the check if the correct role is given because of such things :)
-
@Christian-Ehrlicher Good habit. I need to work on developing one.