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. Signal Slot connection from different classes
Forum Updated to NodeBB v4.3 + New Features

Signal Slot connection from different classes

Scheduled Pinned Locked Moved General and Desktop
20 Posts 8 Posters 22.3k 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.
  • H Offline
    H Offline
    huckfinn
    wrote on last edited by
    #1

    Hello people,

    I have two different classes, lets say Class_A and Class_B, where:

    @
    Class_A::Class_A( Class_B &ref, QWidget *a_parent )
    : QDockWidget( "DOCKW", a_parent )
    , ref2ClassB( ref )
    {
    connect( ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
    }

    void Class_A::slot_Do_Some(int value1, int value2)
    {
    //.. do work
    }
    //EOF
    @

    and in its header:
    @
    class Class_A
    : public QDockWidget
    , public Ui_DockWidget
    {
    Q_OBJECT
    public:
    Class_A( Class_B &ref2b, QWidget *parent);

    private slots:
    

    void slot_Do_Some(int, int);

    private:
        Class_B &ref2ClassB; ///< Reference to Class_B.
    

    };
    #endif
    //EOF
    @

    As you can see in Class_B:
    @
    class Class_B
    {
    private:
    void aMethod();

    signals:
    void trigger_Do_Some(int, int);
    };
    #endif
    @

    and its source:
    @
    void Class_B::aMethod( void )
    {
    // blabla
    int t = 32;
    int r = 45;
    emit trigger_Do_Some(t, r);
    }
    // EOF
    @
    Class_A is parent and contains a reference to Class_B. My intention is following: When I call Class_B::aMethod() SIGNAL trigger_Do_Some(int, int) is being emitted.
    And in the parent class this signal is connected to its slot. Unfortunately I get a error:
    @
    error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'Class_B' to 'const QObject *'
    @

    Does anybody have a solution for me? Thanks in advise

    Huck

    1 Reply Last reply
    1
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Q_OBJECT is missing in Class_B . That should help.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rokemoon
        wrote on last edited by
        #3

        You need that Class_B inherited QObject or derived from it, then add macro Q_OBJECT:
        @class Class_B: public QObject
        {
        Q_OBJECT
        private:
        void aMethod();

        signals:
        void trigger_Do_Some(int, int);
        };@
        and in "connect":http://doc.qt.nokia.com/latest/qobject.html#connect use pointers.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aamer4yu
          wrote on last edited by
          #4

          You need Q_OBJECT declaration in class B.. isnt it ?

          1 Reply Last reply
          0
          • H Offline
            H Offline
            huckfinn
            wrote on last edited by
            #5

            Not really, I just added Q_OBJECT, but I have the same error.
            class Class_B : public QThread, public QObject, also the same error.

            Then I added via Singleton:
            @
            const Class_B * pCB = ref2ClassB.GetInstance();
            connect( pCB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
            @

            Now I can compile, but therefore LNK2001 and LNK2019 link errors.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              huckfinn
              wrote on last edited by
              #6

              Ok, LNK2001 gone, when I removed Q_OBJECT from Class_B. I only let Class_B : public QThread, public QObject

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aamer4yu
                wrote on last edited by
                #7

                Just use
                class Class_B : public QThread

                QThread is already inherited from QObject.
                Does it work ?

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rokemoon
                  wrote on last edited by
                  #8

                  QThread inherits from QObject, so class Class_B : public QThread -,public QObject-

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    huckfinn
                    wrote on last edited by
                    #9

                    Ok, removed.

                    @
                    const Class_B * pCB = ref2ClassB.GetInstance();
                    /// error C2594: 'argument' : ambiguous conversions from 'const Class_B *' to 'const QObject *'
                    connect( pCB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
                    /// error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'Class_B' to 'const QObject *'
                    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
                    connect( ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
                    @

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rokemoon
                      wrote on last edited by
                      #10

                      [quote]
                      All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.
                      [/quote] from "here":http://doc.qt.nokia.com/4.7/signalsandslots.html

                      const Class_B * pCB = ref2ClassB.GetInstance();
                      // here the Class_B must inherit from QOBject
                      connect( pCB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );

                      // here you need the pointer
                      connect( &ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        huckfinn
                        wrote on last edited by
                        #11

                        [quote author="rokemoon" date="1313050079"]
                        // here you need the pointer
                        connect( &ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
                        [/quote]

                        This way I get an error C2594: 'argument' : ambiguous conversions from 'Class_B *' to 'const QObject *'

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rokemoon
                          wrote on last edited by
                          #12

                          Class_B must inherite QObject (directly or indirectly), can you give us latest code of your Class_B?

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            huckfinn
                            wrote on last edited by
                            #13

                            Latest Class_B:
                            @
                            #include <QtCore/QThread>

                            template class SWITCHEXPORT QMap< int, QString >;

                            class Class_A;
                            class QModelIndex;

                            class SWITCHEXPORT Class_B : public QThread {
                            Q_OBJECT

                            public:
                            /// @brief Constructor
                            Class_B( void );

                            void aMethod();

                            signal:
                            void trigger_Do_Some(int, int);

                            private:
                            QMap< int, QString > categoryNameByID;
                            }
                            @

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              rokemoon
                              wrote on last edited by
                              #14

                              Can you tell us why you use QThread? Can you try inherite from QObject and tell us about result?

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                huckfinn
                                wrote on last edited by
                                #15

                                There is a @QThread::start();@ in the source .cpp .

                                So when I inherit from both @clas.s Class_B : public QThread, public QObject@ and I am able to compile it.

                                But now I have linking errors:
                                @
                                3>Linking...
                                3> Creating library C:\MyProject\Debug\MyProject.lib and object C:\MyProject\Debug\MyProject.exp
                                3>Class_B.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Class_B::staticMetaObject" (?staticMetaObject@Class_B@@2UQMetaObject@@B)
                                3>Class_A.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Class_B::staticMetaObject" (?staticMetaObject@Class_B@@2UQMetaObject@@B)
                                3>Class_3.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Class_B::staticMetaObject" (?staticMetaObject@Class_B@@2UQMetaObject@@B)
                                3>Class_B.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Class_B::metaObject(void)const " (?metaObject@Class_B@@UBEPBUQMetaObject@@XZ)
                                3>Class_B.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall Class_B::qt_metacast(char const *)" (?qt_metacast@Class_B@@UAEPAXPBD@Z)
                                3>Class_B.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall Class_B::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Class_B@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
                                3>Class_B.obj : error LNK2019: unresolved external symbol "protected: void __thiscall Class_B::trigger_Do_Some(long,long)" (?trigger_Do_Some@Class_B@@IAEXJJ@Z) referenced in function "public: virtual void __thiscall Class_B::HandleMessage(void * const)" (?HandleMessage@Class_B@@UAEXQAX@Z)
                                3>C:\MyProject\Debug\MyProject.dll : fatal error LNK1120: 5 unresolved externals
                                @

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrtc3
                                  wrote on last edited by
                                  #16

                                  When you have added Q_OBJECT to Class_B.h recently, then assure yourself that moc_ClassB.cxx has been generated as well.
                                  If that is the case, this moc-file should be listed in the .pro or .vcpro file as Source
                                  e.g.:
                                  @
                                  <Files>
                                  <Filter Name="Source Files">
                                  <File RelativePath="C:\path\moc_Class_B.cxx">
                                  </File>
                                  <!-- ....etc...-->
                                  @

                                  There are 10 types. Those who understand binary and those who don't .)

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    huckfinn
                                    wrote on last edited by
                                    #17

                                    I added these tags to the vcpro file, now I can compile and run my application.

                                    However, now I have a
                                    @
                                    warning C4584: 'Class_B' : base-class 'QObject' is already a base-class of 'OtherClass'
                                    @

                                    I dont like warnings :(

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      loladiro
                                      wrote on last edited by
                                      #18

                                      Multiple inheritance is NOT supported for QObject based classes. Can you change your code?

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        Ramin7000
                                        wrote on last edited by
                                        #19

                                        main.cpp :
                                        @
                                        #include <QtGui>
                                        #include <converter.h>

                                        int main(int argc,char *argv[])
                                        {
                                        QApplication app(argc,argv);

                                        QGraphicsScene scene(0,0,500,300);
                                        QGraphicsView view(&scene);
                                        
                                        QLabel *l = new QLabel();
                                        QSlider *s = new QSlider();
                                        
                                        l->setText("Move the slider handle");
                                        s->setOrientation(Qt::Horizontal);
                                        
                                        l->setGeometry(100,100,150,50);
                                        s->setGeometry(100,200,150,50);
                                        
                                        Converter *ms = new Converter(s,l);
                                        
                                        
                                        scene.addWidget(l);
                                        scene.addWidget(s);
                                        
                                        view.show();
                                        return app.exec&#40;&#41;;
                                        

                                        }@

                                        converter.cpp :
                                        @
                                        #include "converter.h"

                                        Converter::Converter(QSlider *s, QLabel *l)
                                        {
                                        connect(s,SIGNAL(valueChanged(int)),this,SLOT(Changer(int)));
                                        connect(this,SIGNAL(Changer(QString)),l,SLOT(setText(QString)));
                                        }

                                        void Converter::Changer(int x)
                                        {
                                        Changer(QString::number(x));
                                        }
                                        @

                                        converter.h :
                                        @
                                        #ifndef CONVERTER_H
                                        #define CONVERTER_H

                                        #include <QtGui>

                                        class Converter : public QObject
                                        {
                                        Q_OBJECT

                                        public:
                                        Converter(QSlider*,QLabel*);

                                        protected slots:
                                        void Changer(int);

                                        signals:
                                        void Changer(QString);

                                        };

                                        #endif // CONVERTER_H
                                        @

                                        [Edit: Please use @ tags around code. I have added them here. -- mlong]

                                        Ramin Lohrasbi

                                        1 Reply Last reply
                                        0
                                        • V Offline
                                          V Offline
                                          vittalonline
                                          wrote on last edited by
                                          #20

                                          Use Q_PROPERTY for passing arguments in Class-B

                                          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