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. New syntax connect not triggering the slot
Forum Updated to NodeBB v4.3 + New Features

New syntax connect not triggering the slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 533 Views 3 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.
  • Puppy BearP Offline
    Puppy BearP Offline
    Puppy Bear
    wrote on last edited by Puppy Bear
    #1

    Hi, I'm trying to use new syntax of connect ,
    but somehow I've found out it couldn't trigger the slot properly while old connect method can.
    However,
    I've checked parameters and Q_OBJECT inheration, connect both return true.

    The slot still couldn't be triggered after emit the signal.
    (qdebug in slot not printing)

    Am I writing any wrong code?

    The code is like
    //all classes inherit q_object

    bms.h

    class XLBmsProtocolPrivate;
    class XLBMSPROTOCOL_SHARED XLBmsProtocol : public QObject
    {
    	Q_OBJECT
    public:
    	XLBmsProtocol(QObject *parent = Q_NULLPTR);
    	~XLBmsProtocol();
    public:
    Q_SIGNAL void sigDCcmd0x65(const int);
    }
    

    bms.cpp

    XLBmsProtocol::XLBmsProtocol(QObject *parent)
    	: QObject(parent)
    	, d_ptr(new XLBmsProtocolPrivate(this))
    {
    	QThread *m_pThread = new QThread;
    	this->moveToThread(m_pThread);
    	m_pThread->start();
    }
    
    void XLBmsProtocol::SlotSendSig(const int num)
    {
    qDebug() << "emit signal"; //always being printed.
    emit sigDCcmd0x65(num);
    }
    

    DCProtocolInitFrm.h

    
    class DCProtocolInitFrm : public DCAbstractProtocol
    {
    	Q_OBJECT
    public:
    	explicit DCProtocolInitFrm(DCProtocolInitFrmPrivate *dd, QWidget *parent = Q_NULLPTR);
    
    protected:
    void InitConnect();
    void DisConnect();
    
    protected
    Q_SLOT virtual void slotDCcmd0x65(const int) Q_DECL_OVERRIDE;
    
    private:
    	Q_DISABLE_COPY(DCProtocolInitFrm)
    	Q_DECLARE_PRIVATE(DCProtocolInitFrm)
    
    

    DCProtocolInitFrm.cpp

    void DCProtocolInitFrm::InitConnect()
    {
    auto tBmsPtr = MyApp->GetProcessPtr();
    
    connect(tBmsPtr, SIGNAL(sigDCcmd0x65(const int)), this, SLOT(slotDCcmd0x65(const int)));  //this works perfectly
    
    /*connect(tBmsPtr, &XLBmsProtocol::sigDCcmd0x65, this,
    		&DCProtocolInitFrm::slotDCcmd0x65);*/ //while this function wasn't working at all.
    }
    
    Q_SLOT void DCProtocolInitFrm::slotDCcmd0x65(const int SkipSignal)
    {
    	Q_D(DCProtocolInitFrm);
            //print sth here, wasn't work under new syntax
    }
    
    JonBJ Gojir4G 3 Replies Last reply
    0
    • Puppy BearP Puppy Bear

      Hi, I'm trying to use new syntax of connect ,
      but somehow I've found out it couldn't trigger the slot properly while old connect method can.
      However,
      I've checked parameters and Q_OBJECT inheration, connect both return true.

      The slot still couldn't be triggered after emit the signal.
      (qdebug in slot not printing)

      Am I writing any wrong code?

      The code is like
      //all classes inherit q_object

      bms.h

      class XLBmsProtocolPrivate;
      class XLBMSPROTOCOL_SHARED XLBmsProtocol : public QObject
      {
      	Q_OBJECT
      public:
      	XLBmsProtocol(QObject *parent = Q_NULLPTR);
      	~XLBmsProtocol();
      public:
      Q_SIGNAL void sigDCcmd0x65(const int);
      }
      

      bms.cpp

      XLBmsProtocol::XLBmsProtocol(QObject *parent)
      	: QObject(parent)
      	, d_ptr(new XLBmsProtocolPrivate(this))
      {
      	QThread *m_pThread = new QThread;
      	this->moveToThread(m_pThread);
      	m_pThread->start();
      }
      
      void XLBmsProtocol::SlotSendSig(const int num)
      {
      qDebug() << "emit signal"; //always being printed.
      emit sigDCcmd0x65(num);
      }
      

      DCProtocolInitFrm.h

      
      class DCProtocolInitFrm : public DCAbstractProtocol
      {
      	Q_OBJECT
      public:
      	explicit DCProtocolInitFrm(DCProtocolInitFrmPrivate *dd, QWidget *parent = Q_NULLPTR);
      
      protected:
      void InitConnect();
      void DisConnect();
      
      protected
      Q_SLOT virtual void slotDCcmd0x65(const int) Q_DECL_OVERRIDE;
      
      private:
      	Q_DISABLE_COPY(DCProtocolInitFrm)
      	Q_DECLARE_PRIVATE(DCProtocolInitFrm)
      
      

      DCProtocolInitFrm.cpp

      void DCProtocolInitFrm::InitConnect()
      {
      auto tBmsPtr = MyApp->GetProcessPtr();
      
      connect(tBmsPtr, SIGNAL(sigDCcmd0x65(const int)), this, SLOT(slotDCcmd0x65(const int)));  //this works perfectly
      
      /*connect(tBmsPtr, &XLBmsProtocol::sigDCcmd0x65, this,
      		&DCProtocolInitFrm::slotDCcmd0x65);*/ //while this function wasn't working at all.
      }
      
      Q_SLOT void DCProtocolInitFrm::slotDCcmd0x65(const int SkipSignal)
      {
      	Q_D(DCProtocolInitFrm);
              //print sth here, wasn't work under new syntax
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Puppy-Bear
      Start with what class is the this in 3.cpp? And it's not helpful that your example uses class names like DCProtocolInitFrm while your example uses A & B, difficult to understand what is actually what.....

      1 Reply Last reply
      1
      • Puppy BearP Puppy Bear

        Hi, I'm trying to use new syntax of connect ,
        but somehow I've found out it couldn't trigger the slot properly while old connect method can.
        However,
        I've checked parameters and Q_OBJECT inheration, connect both return true.

        The slot still couldn't be triggered after emit the signal.
        (qdebug in slot not printing)

        Am I writing any wrong code?

        The code is like
        //all classes inherit q_object

        bms.h

        class XLBmsProtocolPrivate;
        class XLBMSPROTOCOL_SHARED XLBmsProtocol : public QObject
        {
        	Q_OBJECT
        public:
        	XLBmsProtocol(QObject *parent = Q_NULLPTR);
        	~XLBmsProtocol();
        public:
        Q_SIGNAL void sigDCcmd0x65(const int);
        }
        

        bms.cpp

        XLBmsProtocol::XLBmsProtocol(QObject *parent)
        	: QObject(parent)
        	, d_ptr(new XLBmsProtocolPrivate(this))
        {
        	QThread *m_pThread = new QThread;
        	this->moveToThread(m_pThread);
        	m_pThread->start();
        }
        
        void XLBmsProtocol::SlotSendSig(const int num)
        {
        qDebug() << "emit signal"; //always being printed.
        emit sigDCcmd0x65(num);
        }
        

        DCProtocolInitFrm.h

        
        class DCProtocolInitFrm : public DCAbstractProtocol
        {
        	Q_OBJECT
        public:
        	explicit DCProtocolInitFrm(DCProtocolInitFrmPrivate *dd, QWidget *parent = Q_NULLPTR);
        
        protected:
        void InitConnect();
        void DisConnect();
        
        protected
        Q_SLOT virtual void slotDCcmd0x65(const int) Q_DECL_OVERRIDE;
        
        private:
        	Q_DISABLE_COPY(DCProtocolInitFrm)
        	Q_DECLARE_PRIVATE(DCProtocolInitFrm)
        
        

        DCProtocolInitFrm.cpp

        void DCProtocolInitFrm::InitConnect()
        {
        auto tBmsPtr = MyApp->GetProcessPtr();
        
        connect(tBmsPtr, SIGNAL(sigDCcmd0x65(const int)), this, SLOT(slotDCcmd0x65(const int)));  //this works perfectly
        
        /*connect(tBmsPtr, &XLBmsProtocol::sigDCcmd0x65, this,
        		&DCProtocolInitFrm::slotDCcmd0x65);*/ //while this function wasn't working at all.
        }
        
        Q_SLOT void DCProtocolInitFrm::slotDCcmd0x65(const int SkipSignal)
        {
        	Q_D(DCProtocolInitFrm);
                //print sth here, wasn't work under new syntax
        }
        
        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        @Puppy-Bear said in New syntax connect not triggering the slot:

        class C: public Q_OBJECT

        Does this really compile ?

        class C: public Q_OBJECT
        

        It should be

        class C: public QObject {
            Q_OBJECT
            // ... 
        }
        
        JonBJ Puppy BearP 2 Replies Last reply
        0
        • Gojir4G Gojir4

          @Puppy-Bear said in New syntax connect not triggering the slot:

          class C: public Q_OBJECT

          Does this really compile ?

          class C: public Q_OBJECT
          

          It should be

          class C: public QObject {
              Q_OBJECT
              // ... 
          }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Gojir4
          Who knows, because the paste cannot be OP's real code, as classB:public A would be incorrect anyway.....

          1 Reply Last reply
          2
          • J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            also everything is supposed to be in cpp files, which moc does not parse without explicitly being told to do so.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • Puppy BearP Offline
              Puppy BearP Offline
              Puppy Bear
              wrote on last edited by
              #6

              Sorry for the inaccurate description, I've updated the code

              1 Reply Last reply
              0
              • Gojir4G Gojir4

                @Puppy-Bear said in New syntax connect not triggering the slot:

                class C: public Q_OBJECT

                Does this really compile ?

                class C: public Q_OBJECT
                

                It should be

                class C: public QObject {
                    Q_OBJECT
                    // ... 
                }
                
                Puppy BearP Offline
                Puppy BearP Offline
                Puppy Bear
                wrote on last edited by
                #7

                @Gojir4 Sorry for the inaccurate description, the code is being updated and Q_object won't be a problem

                1 Reply Last reply
                0
                • Puppy BearP Puppy Bear

                  Hi, I'm trying to use new syntax of connect ,
                  but somehow I've found out it couldn't trigger the slot properly while old connect method can.
                  However,
                  I've checked parameters and Q_OBJECT inheration, connect both return true.

                  The slot still couldn't be triggered after emit the signal.
                  (qdebug in slot not printing)

                  Am I writing any wrong code?

                  The code is like
                  //all classes inherit q_object

                  bms.h

                  class XLBmsProtocolPrivate;
                  class XLBMSPROTOCOL_SHARED XLBmsProtocol : public QObject
                  {
                  	Q_OBJECT
                  public:
                  	XLBmsProtocol(QObject *parent = Q_NULLPTR);
                  	~XLBmsProtocol();
                  public:
                  Q_SIGNAL void sigDCcmd0x65(const int);
                  }
                  

                  bms.cpp

                  XLBmsProtocol::XLBmsProtocol(QObject *parent)
                  	: QObject(parent)
                  	, d_ptr(new XLBmsProtocolPrivate(this))
                  {
                  	QThread *m_pThread = new QThread;
                  	this->moveToThread(m_pThread);
                  	m_pThread->start();
                  }
                  
                  void XLBmsProtocol::SlotSendSig(const int num)
                  {
                  qDebug() << "emit signal"; //always being printed.
                  emit sigDCcmd0x65(num);
                  }
                  

                  DCProtocolInitFrm.h

                  
                  class DCProtocolInitFrm : public DCAbstractProtocol
                  {
                  	Q_OBJECT
                  public:
                  	explicit DCProtocolInitFrm(DCProtocolInitFrmPrivate *dd, QWidget *parent = Q_NULLPTR);
                  
                  protected:
                  void InitConnect();
                  void DisConnect();
                  
                  protected
                  Q_SLOT virtual void slotDCcmd0x65(const int) Q_DECL_OVERRIDE;
                  
                  private:
                  	Q_DISABLE_COPY(DCProtocolInitFrm)
                  	Q_DECLARE_PRIVATE(DCProtocolInitFrm)
                  
                  

                  DCProtocolInitFrm.cpp

                  void DCProtocolInitFrm::InitConnect()
                  {
                  auto tBmsPtr = MyApp->GetProcessPtr();
                  
                  connect(tBmsPtr, SIGNAL(sigDCcmd0x65(const int)), this, SLOT(slotDCcmd0x65(const int)));  //this works perfectly
                  
                  /*connect(tBmsPtr, &XLBmsProtocol::sigDCcmd0x65, this,
                  		&DCProtocolInitFrm::slotDCcmd0x65);*/ //while this function wasn't working at all.
                  }
                  
                  Q_SLOT void DCProtocolInitFrm::slotDCcmd0x65(const int SkipSignal)
                  {
                  	Q_D(DCProtocolInitFrm);
                          //print sth here, wasn't work under new syntax
                  }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Puppy-Bear said in New syntax connect not triggering the slot:

                  The slot still couldn't be triggered after emit the signal.
                  (qdebug in slot not printing)

                  Start from:

                  connect(tBmsPtr, &XLBmsProtocol::sigDCcmd0x65,
                          this, []() { qDebug("Signal emitted"); });
                  

                  I also note your new code now shows you are using QThread, which you did not mention before. That means you will be using queued connections. Are you allowing the Qt event loop to run? [If the above connect() does not print anything, does it make any difference if you replace this with tBmsPtr?)

                  1 Reply Last reply
                  1

                  • Login

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