Unsolved Signal and Slot connected by slot not called?
-
@J-Hilk , Yes, I'm certain the constructor is called its the only way of creating an instance of this class and just to be absolutely sure I added a boolean to the class that is only set to true after the QObject::connect call.
-
@KroMignon ,there are other areas where the same signal is emitted, its this one particular emit that isn't working.
-
@SPlatten said in Signal and Slot connected by slot not called?:
its this one particular emit that isn't working.
This is not possible,
emit
is in fact just a define of nothing.
Is is just a semantical sugar to simplify code reading, to mark signal usage. It can be removed without any problem.
When compiling code, the moc will create the function behind the signal, and so this is a simple function call.If the generated function for the signal does nothing, then:
- you may have a threading issue, which means the event queue is not called (never ending function or loop)
- the signal use parameter types which are unknown from QMetaType, which is not the case for
QJsonObject
-
@KroMignon ,
This is not possible
That's why I asked, because in other instances it works, but when I step into this with the debugger and have a breakpoint in the slot, it doesn't get there.
In all my thread loops now I have:
QCoreApplication::processEvents();
I've checked the build folder and there isn't a moc_ file for the file this emit is called in.
-
@SPlatten said in Signal and Slot connected by slot not called?:
In all my thread loops now I have:
QCoreApplication::processEvents();This not a good code practice.
As side noteSorry, this is wrong!QCoreApplication::processEvents();
works only if the object you are using are living in the main thread and you are running in the main thread!I've checked the build folder and there isn't a moc_ file for the file this emit is called in.
This does not made sense to me, if you don't have the
moc_
file, then signal code is not generated and it will never work, it will event not compile because you would have a not fully defined class! -
@KroMignon , I can't tell you why, the class header:
/** * File: clsJSON.h * Notes: This file contains the prototype for the clsJSON class. ******************************************************************************* * Class: clsJSON * * Static Methods: * addCommonJSONflds Add common fields in a JSON object * blnDecodeAccordingToType Decode JSON according to type * cstrAppName Access method to get application name * pobjGetScriptEng Access method to get mspobjJSeng * registerDecoder Register a decode function according to type * sendAck Builds and queues Ack message to send * strExplodeJSON Explodes QJson array or object to string * terminateModules Terminates all modules * updateModuleHB Updates a module's heartbeat * * Static Members: * msccCloseCurlyBracket } * msccCloseSquareBracket ] * msccColon : * msccDoubleQuote " * msccKeyDelimiter , * msccOpenCurlyBracket { * msccOpenSquareracket [ * msccSingleQuote ' * mscszAck JSON label: ack * mscszCmdDebug JSON label: dbg * mscszCmdHB JSON command: hearbeat * mscszCmdIndex JSON label: cmdidx * mscszCmdInitial init * mscszCmdNotify notify * mscszCmdReady ready * mscszCommand JSON label: command * mscszCommands JSON label: commands * mscszCRLF \r\n * mscszDecoder JSON label: decoder * mscszError JSON label: error * mscszFound JSON label: found * mscszKeys JSON label: keys * mscszMatch JSON label: match * mscszMember JSON label: member * mscszModule module * mscszMsgID msgID * mscszMsgType msgType * mscszPID PID * mscszScriptFile JSON label: script * mscszSource JSON label: source * mscszSkipOver JSON label: skipover * mscszText JSON label: text * mscszTime JSON label: time * mscszTrue JSON label: true * mscszType JSON label: type * mscszType ... JSON conversion types * mscszWhitespace "\t\n\v\f\r " * mslstDecoders List of JSON decoders * msmpSockets Map of JSON sockets * mspobjJSeng Pointer to instance of script engine * * Methods: * clsJSON Class constructor * ~clsJSON Class destructor * blnFind Returns true if name found else false * blnIsArray Returns true if JSON is an array * blnIsObject Returns true if JSON is an object * blnIsValid Returns true if JSON object is valid else false * fromByteArray Decodes passed byte array into JSON object * fromString Decodes passed string into JSON object * strToString Constructs JSON string * strURIdecode Decoded string * toQJsonObject Returns instance of QJsonObject * * Members: * maryJSON Array of JSON objects * mbytarrJSON Byte array containing the JSON for the object * mobjJSON Object containing JSON ******************************************************************************* * History: 2019/05/03 Created by Simon Platten */ #ifndef CLSJSON_H #define CLSJSON_H #include <map> #include <QAbstractSocket> #include <QProcess> #include <QString> #include <QStringList> #include <QJSEngine> #include <QJSValue> #include <QJsonArray> #include <QJsonDocument> #include <QJsonObject> #include <QJsonValue> #include "clsDebugService.h" #include "clsModule.h" typedef bool(*pfnDecodeJSON)(const QJsonObject& crobjJSON); typedef std::map<QString, QMetaObject::Connection> mpConnections; typedef QList<QJsonObject> lstDecoders; class clsJSON { private: QJsonDocument mdocJSON; static QJSEngine* mspobjJSeng; static lstDecoders mslstDecoders; QString strToJSONX(QJsonValueRef refJSON); public: static const char msccCloseCurlyBracket = '}'; static const char msccCloseSquareBracket = ']'; static const char msccColon = ':'; static const char msccDoubleQuote = '\"'; static const char msccKeyDelimiter = ','; static const char msccOpenCurlyBracket = '{'; static const char msccOpenSquareracket = '['; static const char msccSingleQuote = '\''; static const int mscintQtVersion; static const char mscszAck[]; static const char mscszCommand[]; static const char mscszCommands[]; static const char mscszCmdDebug[]; static const char mscszCmdHB[]; static const char mscszCmdIndex[]; static const char mscszCmdInitial[]; static const char mscszCmdNotify[]; static const char mscszCmdReady[]; static const char mscszCRLF[]; static const char mscszDecoder[]; static const char mscszError[]; static const char mscszFound[]; static const char mscszKeys[]; static const char mscszMatch[]; static const char mscszMember[]; static const char mscszModule[]; static const char mscszMsgID[]; static const char mscszMsgType[]; static const char mscszPID[]; static const char mscszScriptFile[]; static const char mscszSource[]; static const char mscszSkipOver[]; static const char mscszText[]; static const char mscszTime[]; static const char mscszTrue[]; static const char mscszType[]; static const char mscszTypeChar[]; static const char mscszTypeDouble[]; static const char mscszTypeFloat[]; static const char mscszTypeInt[]; static const char mscszTypeLong[]; static const char mscszTypeShort[]; static const char mscszTypeString[]; static const char mscszTypeUChar[]; static const char mscszTypeUInt[]; static const char mscszTypeULong[]; static const char mscszTypeUShort[]; static const char mscszWhitespace[]; clsJSON(const QByteArray& crbytarrJSON); clsJSON(const QString& crstrJSON); ~clsJSON(); static void addCommonJSONflds(QJsonObject& robjJSON ,const char* pszMsgType = nullptr ,clsModule* pobjModule = nullptr); static bool blnDecodeAccordingToType(const QJsonObject& crobjSubject); bool blnFind(const QString& crstrName, QJsonValue* pobjFound = nullptr); bool blnIsArray() { return mdocJSON.isArray(); } bool blnIsObject() { return mdocJSON.isObject(); } bool blnIsValid(); void fromByteArray(const QByteArray& crbytarrJSON); void fromString(const QString& crpstrJSON); static QJSEngine* pobjGetScriptEng(); static void registerDecoder(const QJsonObject& crobjDecoder); static void sendAck(const QJsonObject& crobjJSON); #if defined(DEBUG_JSON) static QString strExplodeJSON(const QJsonArray& craryJSON, quint8 uint8Level); static QString strExplodeJSON(const QJsonObject& crorbJSON, quint8 uint8Level); #endif QString strToString(); QString strURIdecode(const char* pszData); static void terminateModules(); QJsonObject toQJsonObject(); static void updateModuleHB(const QString& crstrAlias); }; //JSON packet decodes #if defined(MODULE_BUILD) bool blnDecodeAck(const QJsonObject& crobjJSON); bool blnDecodeNotify(const QJsonObject& crobjJSON); #else bool blnDecodeDebug(const QJsonObject& crobjJSON); bool blnDecodeHeartbeat(const QJsonObject& crobjJSON); bool blnDecodeReady(const QJsonObject& crobjJSON); #endif #endif // CLSJSON_H
In the build folder there is:
clsJSON.o
But no mov_clsJSON.o
-
@SPlatten said in Signal and Slot connected by slot not called?:
But no mov_clsJSON.o
Of course there is not moc file for this class, it has no
Q_OBJECT
, nor it is a QObject sub-class , nor it have signals or slots!In you code extract, you are talking about
clsModule
notclsJSON
, now I am again totally lost... what is the purpose of your question? -
@KroMignon , here is the content of the file moc_clsModule.cpp:
/**************************************************************************** ** Meta object code from reading C++ file 'clsModule.h' ** ** Created by: The Qt Meta Object Compiler version 67 (Qt 5.15.2) ** ** WARNING! All changes made in this file will be lost! *****************************************************************************/ #include <memory> #include "../clsModule.h" #include <QtCore/qbytearray.h> #include <QtCore/qmetatype.h> #if !defined(Q_MOC_OUTPUT_REVISION) #error "The header file 'clsModule.h' doesn't include <QObject>." #elif Q_MOC_OUTPUT_REVISION != 67 #error "This file was generated using the moc from 5.15.2. It" #error "cannot be used with the include files from this version of Qt." #error "(The moc has changed too much.)" #endif QT_BEGIN_MOC_NAMESPACE QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED struct qt_meta_stringdata_clsModule_t { QByteArrayData data[12]; char stringdata0[106]; }; #define QT_MOC_LITERAL(idx, ofs, len) \ Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \ qptrdiff(offsetof(qt_meta_stringdata_clsModule_t, stringdata0) + ofs \ - idx * sizeof(QByteArrayData)) \ ) static const qt_meta_stringdata_clsModule_t qt_meta_stringdata_clsModule = { { QT_MOC_LITERAL(0, 0, 9), // "clsModule" QT_MOC_LITERAL(1, 10, 3), // "PID" QT_MOC_LITERAL(2, 14, 0), // "" QT_MOC_LITERAL(3, 15, 11), // "crstrProces" QT_MOC_LITERAL(4, 27, 8), // "int64PID" QT_MOC_LITERAL(5, 36, 7), // "sendAck" QT_MOC_LITERAL(6, 44, 9), // "crobjJSON" QT_MOC_LITERAL(7, 54, 8), // "sendJSON" QT_MOC_LITERAL(8, 63, 12), // "QJsonObject&" QT_MOC_LITERAL(9, 76, 8), // "robjJSON" QT_MOC_LITERAL(10, 85, 9), // "onSendAck" QT_MOC_LITERAL(11, 95, 10) // "onSendJSON" }, "clsModule\0PID\0\0crstrProces\0int64PID\0" "sendAck\0crobjJSON\0sendJSON\0QJsonObject&\0" "robjJSON\0onSendAck\0onSendJSON" }; #undef QT_MOC_LITERAL static const uint qt_meta_data_clsModule[] = { // content: 8, // revision 0, // classname 0, 0, // classinfo 5, 14, // methods 0, 0, // properties 0, 0, // enums/sets 0, 0, // constructors 0, // flags 3, // signalCount // signals: name, argc, parameters, tag, flags 1, 2, 39, 2, 0x06 /* Public */, 5, 1, 44, 2, 0x06 /* Public */, 7, 1, 47, 2, 0x06 /* Public */, // slots: name, argc, parameters, tag, flags 10, 1, 50, 2, 0x0a /* Public */, 11, 1, 53, 2, 0x0a /* Public */, // signals: parameters QMetaType::Void, QMetaType::QString, QMetaType::LongLong, 3, 4, QMetaType::Void, QMetaType::QJsonObject, 6, QMetaType::Void, 0x80000000 | 8, 9, // slots: parameters QMetaType::Void, QMetaType::QJsonObject, 6, QMetaType::Void, 0x80000000 | 8, 9, 0 // eod }; void clsModule::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a) { if (_c == QMetaObject::InvokeMetaMethod) { auto *_t = static_cast<clsModule *>(_o); Q_UNUSED(_t) switch (_id) { case 0: _t->PID((*reinterpret_cast< const QString(*)>(_a[1])),(*reinterpret_cast< const qint64(*)>(_a[2]))); break; case 1: _t->sendAck((*reinterpret_cast< const QJsonObject(*)>(_a[1]))); break; case 2: _t->sendJSON((*reinterpret_cast< QJsonObject(*)>(_a[1]))); break; case 3: _t->onSendAck((*reinterpret_cast< const QJsonObject(*)>(_a[1]))); break; case 4: _t->onSendJSON((*reinterpret_cast< QJsonObject(*)>(_a[1]))); break; default: ; } } else if (_c == QMetaObject::IndexOfMethod) { int *result = reinterpret_cast<int *>(_a[0]); { using _t = void (clsModule::*)(const QString & , const qint64 & ); if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&clsModule::PID)) { *result = 0; return; } } { using _t = void (clsModule::*)(const QJsonObject & ); if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&clsModule::sendAck)) { *result = 1; return; } } { using _t = void (clsModule::*)(QJsonObject & ); if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&clsModule::sendJSON)) { *result = 2; return; } } } } QT_INIT_METAOBJECT const QMetaObject clsModule::staticMetaObject = { { QMetaObject::SuperData::link<QProcess::staticMetaObject>(), qt_meta_stringdata_clsModule.data, qt_meta_data_clsModule, qt_static_metacall, nullptr, nullptr } }; const QMetaObject *clsModule::metaObject() const { return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject; } void *clsModule::qt_metacast(const char *_clname) { if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_clsModule.stringdata0)) return static_cast<void*>(this); return QProcess::qt_metacast(_clname); } int clsModule::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QProcess::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { if (_id < 5) qt_static_metacall(this, _c, _id, _a); _id -= 5; } else if (_c == QMetaObject::RegisterMethodArgumentMetaType) { if (_id < 5) *reinterpret_cast<int*>(_a[0]) = -1; _id -= 5; } return _id; } // SIGNAL 0 void clsModule::PID(const QString & _t1, const qint64 & _t2) { void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(std::addressof(_t1))), const_cast<void*>(reinterpret_cast<const void*>(std::addressof(_t2))) }; QMetaObject::activate(this, &staticMetaObject, 0, _a); } // SIGNAL 1 void clsModule::sendAck(const QJsonObject & _t1) { void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(std::addressof(_t1))) }; QMetaObject::activate(this, &staticMetaObject, 1, _a); } // SIGNAL 2 void clsModule::sendJSON(QJsonObject & _t1) { void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(std::addressof(_t1))) }; QMetaObject::activate(this, &staticMetaObject, 2, _a); } QT_WARNING_POP QT_END_MOC_NAMESPACE
The reason I posted clsJSON.h is that the emit that isn't working is called from a static method of this class:
void clsJSON::sendAck(const QJsonObject& crobjJSON) { QJsonObject::const_iterator citrCmdType = crobjJSON.find(clsJSON::mscszMsgType) ,citrSourceFound = crobjJSON.find(clsJSON::mscszSource); if ( citrCmdType == crobjJSON.end() || citrSourceFound == crobjJSON.end() ) { return; } const QString cstrMsgType = citrCmdType->toString() ,cstrSource = citrSourceFound->toString(); clsModule* pobjModule = clsModule::pGetModule(cstrSource); if ( pobjModule == nullptr ) { return; } //Build up acknowedge response QJsonObject objJSON; //Add the modified acknowledge message type QString strAckMsgType(clsJSON::mscszAck + cstrMsgType); objJSON.insert(clsJSON::mscszMsgType, strAckMsgType); //This needs to be XMLMPAM as its the only app that can acknowledge (At this time)! objJSON.insert(clsJSON::mscszSource, clsDebugService::cstrPrefix()); emit pobjModule->sendJSON(objJSON); }
-
@SPlatten said in Signal and Slot connected by slot not called?:
The reason I posted clsJSON.h is that the emit that isn't working is called from a static method of this class:
Again:
-
emit is a semantical sugar, it has no signification, it is only for the reader to identify usage of a signal
emit pobjModule->sendJSON(objJSON);
is absolutely the same aspobjModule->sendJSON(objJSON);
You are simply call a member, nothing more, nothing less. -
signals are in fact functions
-
corresponding function for a signal for a given class is automatically generated by moc during compilation
-
this generated function is always a public function in the class (in your example in
clsModule
)
Typically implementation will record slot call in event loop of the thread from receiver.
So to ensure slot will be called, the event loop of the receiver must not be locked.I don't know how to explain it better.
-
-
You dont use Signals&Slots in
clsJSON
directly, which is totally fine, but AFAIK you also can't emit signals in other classes (which do inherit fromQObject
, have macro and so on), when not having theQ_OBJECT
macro. This is the case here, I guess. -
@KroMignon said in Signal and Slot connected by slot not called?:
emit is a semantical sugar, it has no signification, it is only for the reader to identify usage of a signal
emit pobjModule->sendJSON(objJSON); is absolutely the same as pobjModule->sendJSON(objJSON);
You are simply call a member, nothing more, nothing less.Allow me to interpret this reoccurring "sugary " statement
emit pobjModule->sendJSON(objJSON);
using "emit" is therefore equivalent to
int a = 10; /// set variable a to 10
where
/// set variable a to 10 is a superficial sugar telling nothing about what "a" is /does
IMHO
repeating the "emit does nothing , it is a sugar " directly discourages to write readable code , unless the coder objective is to write obfuscated stuff for false "job security".
Cheers
' -
@AnneRanch said in Signal and Slot connected by slot not called?:
repeating the "emit does nothing , it is a sugar " directly discourages to write readable code , unless the coder objective is to write obfuscated stuff for false "job security".
Cheers
'You are misunderstanding my comments.
All I want to say with "semantical sugar", is that there is nothing which is magically done here.
If emitting signal is working somewhere, it will work everywhere.And if the slot is not called by one specific call only, it simply means that the thread event loop is not working/locked.
I am not an English native speaker, so I try to explain the best I can.
-
@KroMignon , thank you, np.
-
- Write a
qDebug()
message just after theQObject::connect(this, &clsModule::sendJSON, this, &clsModule::onSendJSON);
. - Write a
qDebug()
message just after theemit pobjModule->sendJSON(objJSON);
- Write a
qDebug()
message at the start just after theclsModule::onSendJSON()
Questions:
- When you run your program, which debug messages above get printed to output?
- Which thread does your
clsModule
object live in? - Which thread do you call
clsJSON::sendAck()
from? - Why does
clsModule
inheritQProcess
?
- Write a
-
@KroMignon said in Signal and Slot connected by slot not called?:
I am not an English native speaker, so I try to explain the best I can.
Neither am I .
Telling me I do not understand what you wrote is not the subject of discussion.
And it works BOTH ways.Let me try it another way - "emit" is not a keyword.
It is a macro and has a purpose / function in the code.
Its placement is important.BUT if majority of forum participators will keep downgrading "emit" to "sugar" it does not mean such attitude is the best.
Just to make sure - I object ONLY to your treatment of "emit" , I have no comment to the rest of you post.
-
@AnneRanch said in Signal and Slot connected by slot not called?:
Let me try it another way - "emit" is not a keyword.
true
It is a macro
true(ish)
and has a purpose / function in the code.
false
Its placement is important.
false
here's the definition of emit
#ifndef QT_NO_EMIT # define emit #endif
its quite literally expands to nothing
-
@AnneRanch said in Signal and Slot connected by slot not called?:
It is a macro and has a purpose / function in the code.
It's just for debug purposes and to increase the readability. You can say from looking at the emit, that you "call" (or emit) a signal next. Without, it would be hard(er) to say, where exactly you emit your signals. You would need to follow the trace of every signal.
-
@JKSH said in Signal and Slot connected by slot not called?:
- Write a
qDebug()
message just after theQObject::connect(this, &clsModule::sendJSON, this, &clsModule::onSendJSON);
. - Write a
qDebug()
message just after theemit pobjModule->sendJSON(objJSON);
- Write a
qDebug()
message at the start just after theclsModule::onSendJSON()
Questions:
- When you run your program, which debug messages above get printed to output?
- Which thread does your
clsModule
object live in? - Which thread do you call
clsJSON::sendAck()
from? - Why does
clsModule
inheritQProcess
?
I would also suggest adding to the debug log the result of the connection to verify that the connection is actually done correctly. Also, I have a question. On your output log do you see some log related to the module clsModule not being registered using qRegisterMetaType() or anything like that when you try to perform your connection? It may be that we need to register this module as a meta type in order to use it on the connections.
- Write a
-
@rrlopez , what makes you say I need to register clsModule as a type? I don't pass that as a signal parameter I only pass QJsonObject.
-
I was just asking if you had a message on your output log. If you don't get that message then you don't need to register clsModule as a type. What interest me the most is the result of the connection. If you create a QMetaObject::Connection object and assign it to the connection you're trying to do, is it valid? you can check that by doing something like this:
QMetaObject::Connection testConnection = QObject::connect(this, &clsModule::sendJSON, this, &clsModule::onSendJSON); if (!testConnection) { // handle testConnection invalid case (or print error message) }