Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to connect Signal / Slot with inheritence

How to connect Signal / Slot with inheritence

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 515 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by Infinity
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      I 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @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.

        I Offline
        I Offline
        Infinity
        wrote on last edited by
        #3

        @Christian-Ehrlicher What do you mean with "use another slot"? I already have two different slots.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          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?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          I 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            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?

            I Offline
            I Offline
            Infinity
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Infinity said in How to connect Signal / Slot with inheritence:

              How should I do this?

              See QObject::disconnect()

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              I 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Infinity said in How to connect Signal / Slot with inheritence:

                How should I do this?

                See QObject::disconnect()

                I Offline
                I Offline
                Infinity
                wrote on last edited by
                #7

                @Christian-Ehrlicher Thank you very much. That solved my issue.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved