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. Qt5 new signal/slot connection syntax

Qt5 new signal/slot connection syntax

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 937 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.
  • mistralegnaM Offline
    mistralegnaM Offline
    mistralegna
    wrote on last edited by mistralegna
    #1

    Hello,

    If I have a class B derived from class A, where class A declares signals and implements virtual slots, is there a way to connect the overriden slots of an instance of class B stored as a pointer on A with the new syntax QObject::connect ?

    Thank you by advance!

    jsulmJ 1 Reply Last reply
    0
    • mistralegnaM mistralegna

      Hello,

      If I have a class B derived from class A, where class A declares signals and implements virtual slots, is there a way to connect the overriden slots of an instance of class B stored as a pointer on A with the new syntax QObject::connect ?

      Thank you by advance!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mistralegna I don't know for sure, but you can just try and see how it behaves?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        It works out of the box...
        The example below will print "b"

        a.h

        #ifndef A_H
        #define A_H
        #include <QObject>
        #include <QDebug>
        class A : public QObject
        {
            Q_OBJECT
        public:
            explicit A(QObject *parent = nullptr) : QObject(parent){}
            virtual ~A() = default;
        public slots:
            virtual void test(){
                qDebug("a");
            }
        
        
        };
        #endif // A_H
        

        b.h

        #ifndef B_H
        #define B_H
        
        #include "a.h"
        
        class B : public A
        {
            Q_OBJECT
        public:
            explicit B(QObject *parent = nullptr) : A(parent){};
            virtual ~B() = default;
        public slots:
            virtual void test() override{
                qDebug("b");
            }
        };
        
        #endif // B_H
        

        main.cpp

        #include <QCoreApplication>
        #include "b.h"
        #include <QTimer>
        
        
        int main(int argc, char *argv[]) {
            QCoreApplication appl(argc,argv);
            A* testingB=new B();
            QTimer testTimer;
            testTimer.setSingleShot(true);
            QObject::connect(&testTimer,&QTimer::timeout,testingB,&A::test);
            testTimer.start(100);
            appl.exec();
            delete testingB;
            return 0;
        }
        
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • mistralegnaM Offline
          mistralegnaM Offline
          mistralegna
          wrote on last edited by
          #4

          Thank you very much for your answer.

          Is it possible to use this syntax without having to include the headers where the signals/slots are defined ?

          Thank you by advance!

          VRoninV 1 Reply Last reply
          0
          • mistralegnaM mistralegna

            Thank you very much for your answer.

            Is it possible to use this syntax without having to include the headers where the signals/slots are defined ?

            Thank you by advance!

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            @mistralegna In my example you can #include "a.h" instead of #include "b.h" in main.cpp as long as testingB is created somewhere else

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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