[Interest] Connecting output of an external process to a QML component

Alejandro Exojo suy at badopi.org
Thu Jan 1 16:20:09 CET 2015


El Wednesday 31 December 2014, Daniel França escribió:
> I'm instantiating the C++ class via setContextProperty, so it's a singleton
> object.

I would not say that this is a singleton object. You register one already 
instantated object instead of a class, but it could be or not a singleton.

> Then I start the process, and connect the signal/slot:
> http://pastebin.com/p2CyZwfh
> 
> Here's the first problem... the slot is never being called.
> The slot should be called and then emit another signal (that I could
> connect to QML): http://pastebin.com/Zvj1Y11w
> 
> The other problem is that QML can't recognize this signal:
> http://pastebin.com/QWkkk5mT
> Giving me the error: *Cannot call method 'connect' of undefined*
> and of course, the signal is defined: http://pastebin.com/rxsnQNke

This is a horrible way to show your code. You are pasting 2 line snippets 
without any context.

I see the same mistake that Dmitry saw for starters: in QML you have to follow 
Qt's conventions or is likely that you are doing it wrong. Only types begin 
with "Capital" letter. Not only that. In the other paste you are writing it 
beginning with a lower case first letter.
 
> Googling around all the examples I see are using the QMLRegisterType
> instead, to connect signals/slots, should I move to this way? It's not
> possible to connect with singleton/context objects?

Unrelated. Think of the way you are using a way to make one object alone (or 
several if you instantiate more, but one at a time). the qmlRegisterType is 
for registering a class as a QML object type.
 
> Of course, if there's an easier way to show the standard output of another
> process to a QML component I would love to know.

Depends on what you want to achieve. If for example you want to launch a 
process and show it's stdout in a TextEdit element I would wrap the QProcess 
in a subclass that keeps a Q_PROPERTY updated with the contents of that 
output. That would be probably more efficient, since instead of passing a 
string list through the signal, you follow the usual pattern of signaling only 
that new content is available, and using a "getter" to get those conents, 
instead of passing them all in the signal. That's what Qt does (e.g. your 
QProcess doesn't pass all the output contents in the signal; you react to the 
signal and call readAllStandardOutput() in the slot).

-- 
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net



More information about the Interest mailing list