New syntax connect not triggering the slot
-
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_objectbms.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 }
-
@Puppy-Bear
Start with what class is thethis
in3.cpp
? And it's not helpful that your example uses class names likeDCProtocolInitFrm
while your example usesA
&B
, difficult to understand what is actually what..... -
@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 // ... }
-
also everything is supposed to be in cpp files, which moc does not parse without explicitly being told to do so.
-
Sorry for the inaccurate description, I've updated the code
-
@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 aboveconnect()
does not print anything, does it make any difference if you replacethis
withtBmsPtr
?)