qmlRegisterSingletonType object emit signal. Problem with qml slot connection
-
Hi, i've a qmlRegisterSingletonType object that's can emit two signals. I want capture this signal with slots in QML.
This is the main:
qmlRegisterSingletonType<SDK2220_SysLog>("VidexMessageBox",0,1,"SysLogSocket",Get_SysLogManager);
This is my class :
class SDK2220_SysLog :public QThread { Q_OBJECT Q_PROPERTY(NOTIFY set_logger_okay CONSTANT) Q_PROPERTY(NOTIFY add_new_item CONSTANT) public: SDK2220_SysLog(); SDK2220_SysLog(QHostAddress *addr,int port); private slots: void on_start_thread_emitted(); signals: Q_INVOKABLE void set_logger_okay(); Q_INVOKABLE void add_new_item(); };
and this is a QML CODE:
Connections{ target: SysLogSocket onSet_logger_okay:{ setting.saveFilterChecked("SRV2220",2) console.log("33333333333333333333333333333333333333333333333333333") } }
When in the cpp code i emit set_logger_okay, it doesen't captured to QML.
Someone can help me?
-
Check if there is any warning printed at runtime - perhaps Connections did not really connect?
Also, try removing the Q_PROPERTY notifiers - just having signals is enough. And you don't need to marks signals as Q_INVOKABLE, they already are invokable.
-
@edoardo-videx said in qmlRegisterSingletonType object emit signal. Problem with qml slot connection:
Get_SysLogManager
please show us your singleton instance function
-
static QObject * Get_SysLogManager(QQmlEngine * engine, QJSEngine *scriptEngine) { Q_UNUSED(engine); Q_UNUSED(scriptEngine); static SDK2220_SysLog * s_instance = new SDK2220_SysLog(); QQmlEngine::setObjectOwnership(s_instance, QQmlEngine::CppOwnership); return s_instance; }
-
@edoardo-videx alight
and how and when do you emit the signal from c++ ? -
Are you 100% sure the signal is actually being emitted? Throw in some debug message to verify.
-
Also, does that
run()
method leave some space for even loop to propagate the signal? -
@edoardo-videx I see SDK220_SysLog got a slot/method called
run()
. Is it sub-classingQObject
orQThread
?
To made signal/slots works, a QObject class must be running in a thread which implements a event loop... If you have sub-classed QThread and then also the QThread::run() method, then there will be no event queue to handle the signal!You can try to create a temporary QEventLoop to handle your signal:
if( evt->cmd == SDK2220_PROTOCOL_CMD_SET_LOGGER) { QEventLoop loop; Q_UNUSED(loop) emit set_logger_okay(); }
In general, it is not a good practice to sub-class QThread, it is bette to create a worker class and to move this class to new thread.
Take a look at following links for more details:
http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/
https://www.toptal.com/qt/qt-multithreading-c-plus-plus -
@KroMignon said in qmlRegisterSingletonType object emit signal. Problem with qml slot connection:
QEventLoop loop;
Q_UNUSED(loop)I don't even know what this loop is supposed to do as exec() isn't called here