Solved How to connect Signal / Slot with inheritence
-
I created a custom QSqlTableModel class. It looks like this:
mysqltablemodelreadonly.h
#include <QObject> #include <QDebug> #include <QItemSelection> #include <QSqlTableModel> class MySqlTableModelReadOnly : public QSqlTableModel { Q_OBJECT public: explicit MySqlTableModelReadOnly(const QString table, QObject *parent = nullptr); void setSelection(const QItemSelection selection); private: QItemSelection m_selection; signals: void selectionChanged(); };
mysqltablemodelreadonly.cpp
MySqlTableModelReadOnly::MySqlTableModelReadOnly(const QString table, QObject *parent) : QSqlTableModel(parent) { this->setTable(table); } void MySqlTableModelReadOnly::setSelection(const QItemSelection selection) { // This selection is set by the view m_selection = selection; emit this->selectionChanged(); }
I created two classes which inherit MySqlTableModelReadOnly:
MySqlModelProfile::MySqlModelProfile(QObject *parent) : MySqlTableModelReadOnly("profile", parent) { connect(this, &MySqlModelProfile::selectionChanged, this, &MySqlModelProfile::on_profileSelectionChanged); }
and:
MySqlModelAccount::MySqlModelAccount(QObject *parent) : MySqlTableModelReadOnly("account", parent) { connect(this, &MySqlModelAccount::selectionChanged, this, &MySqlModelAccount::on_accountSelectionChanged); }
When MySqlTableModelReadOnly::selectionChanged() is emitted both classes which inherit MySqlTableModelReadOnly catch the signal on_selectionChanged.
How do I have to connect the slots, that just the child class receives the signal?
-
@Infinity said in How to connect Singal / Slot with inheritence:
How do I have to connect the slots, that just the child class receives the signal?
Use another slot.
-
@Christian-Ehrlicher What do you mean with "use another slot"? I already have two different slots.
-
Then disconnect the other one.
But on the other side - your design is flawed - why don't you want that your base class does it's work? -
@Christian-Ehrlicher said in How to connect Signal / Slot with inheritence:
But on the other side - your design is flawed - why don't you want that your base class does it's work?
How should I do this? I don't have much experience with this.
-
@Infinity said in How to connect Signal / Slot with inheritence:
How should I do this?
-
@Christian-Ehrlicher Thank you very much. That solved my issue.