Solved QSqlTableModel::select() override
-
I have created a subclass of QSqlTableModel and would like to override QSqlTableModel::select() in this subclass. My override select() looks like this:
public slots: bool select() override;
bool MySqlTableModelReadOnly::select() { emit this->selectionChanged(); return QSqlTableModel::select(); }
Is that the way how to do this, or is there a cleaner way to do it?
-
hi
Yes if the goal is to emit a signal and
then do what normally base class do in select, it looks fine. -
Perfect. Thank you very much.
-
Hi
Just a note.
You might want to do like.bool MySqlTableModelReadOnly::select() { bool result = QSqlTableModel::select(); emit this->selectionChanged(); return result; }
to make sure the signal comes after its actually has loaded data if the responder
to the signal starts to look at data.
Not sure it matters though. It depends. -
@mrjj Thanks. I will do that.
-
@Infinity , @mrjj
Am I going mad? As per https://doc.qt.io/qt-5/qsqltablemodel-members.html, there is noQSqlTableModel::select()
to override, nor anyQSqlTableModel::selectionChanged()
signal? -
@JonB there is for select, it's a virtual slot.
However, there's indeed nothing for selectionChanged because it not the role of the model itself to manage selection. You can find that signal at the view level and the selection model level.
-
@SGaist said in QSqlTableModel::select() override:
@JonB there is for select, it's a virtual slot
(https://doc.qt.io/qt-5/qsqltablemodel.html#select)
Hmm. When I click for "This is the complete list of members for QSqlTableModel, including inherited members.", https://doc.qt.io/qt-5/qsqltablemodel-members.html, I expect them to be alphabetical. I have only just now noticed that they seem to go through alphabetically (at least) twice...! I only looked in second column forselect
. This might explain why in the past I have sometimes had problems locating something in that list.... -
@JonB QSqlTableModel::selectionChanged() does not exist. It is just a signal which I implemented. That is the reason why I wanted to override the QSqlTableModel::select() slot. Sorry for the confusion.
mysqltablemodelreadonl.h
class MySqlTableModelReadOnly : public QSqlTableModel { Q_OBJECT public: explicit MySqlTableModelReadOnly(QObject *parent = nullptr); private: signals: void selectionChanged(); public slots: bool select() override; };
mysqltablemodelreadonl.cpp
MySqlTableModelReadOnly::MySqlTableModelReadOnly(QObject *parent) : QSqlTableModel(parent) { } bool MySqlTableModelReadOnly::select() { bool result = QSqlTableModel::select(); emit this->selectionChanged(); return result; }