'Unable to get output of a Python script with qprocess
I am trying to get the output of a Python script with qprocess but I can't seem to make it work. I think the issue is with the script since I can get the output of a test script I wrote without a problem. I don't know enough about python to see what the issue is. What might be the problem?
My C++ qt program:
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString monitor = "/home/monitor-ofono.py";
// QString monitor = "/home/test3.py";
myprocess = new QProcess(this);
connect(myprocess, SIGNAL(readyReadStandardOutput()),
this, SLOT(readOutput()));
myprocess->setProcessChannelMode(QProcess::MergedChannels);
myprocess->start(monitor);
//myprocess->waitForFinished();
qDebug() << "error code:" << myprocess->error();
}
void MainWindow::readOutput()
{
QString textOut = myprocess->readAllStandardOutput();
emit myOutput(textOut);
ui->textEdit->setText(textOut);
}
My test script that works
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 4 12:03:17 2022
@author: me
"""
import time
import sys
for i in range(5):
print(i),
sys.stdout.flush()
time.sleep(1)
The script I am trying to get the output from returns nothing to qprocess but works fine in a terminal
#!/usr/bin/python3
from gi.repository import GLib
import dbus
import dbus.mainloop.glib
_dbus2py = {
dbus.String : str,
dbus.UInt32 : int,
dbus.Int32 : int,
dbus.Int16 : int,
dbus.UInt16 : int,
dbus.UInt64 : int,
dbus.Int64 : int,
dbus.Byte : int,
dbus.Boolean : bool,
dbus.ByteArray : str,
dbus.ObjectPath : str
}
def dbus2py(d):
t = type(d)
if t in _dbus2py:
return _dbus2py[t](d)
if t is dbus.Dictionary:
return dict([(dbus2py(k), dbus2py(v)) for k, v in d.items()])
if t is dbus.Array and d.signature == "y":
return "".join([chr(b) for b in d])
if t is dbus.Array or t is list:
return [dbus2py(v) for v in d]
if t is dbus.Struct or t is tuple:
return tuple([dbus2py(v) for v in d])
return d
def pretty(d):
d = dbus2py(d)
t = type(d)
if t in (dict, tuple, list) and len(d) > 0:
if t is dict:
d = ", ".join(["%s = %s" % (k, pretty(v))
for k, v in d.items()])
return "{ %s }" % d
d = " ".join([pretty(e) for e in d])
if t is tuple:
return "( %s )" % d
if t is str:
return "%s" % d
return str(d)
def property_changed(name, value, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s = %s" % (iface, path, name, pretty(value)))
def added(name, value, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s" % (iface, member, name, pretty(value)))
def removed(name, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s" % (iface, name, member))
def event(member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s" % (iface, path, member))
def message(msg, args, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s (%s)" % (iface, path, member,
msg, pretty(args)))
def ussd(msg, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s" % (iface, path, member, msg))
def value(value, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s" % (iface, path, member, str(value)))
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(property_changed,
bus_name="org.ofono",
signal_name = "PropertyChanged",
path_keyword="path",
interface_keyword="interface")
for member in ["IncomingBarringInEffect",
"OutgoingBarringInEffect",
"NearMaximumWarning"]:
bus.add_signal_receiver(event,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["ModemAdded",
"ContextAdded",
"CallAdded",
"MessageAdded"]:
bus.add_signal_receiver(added,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["ModemRemoved",
"ContextRemoved",
"CallRemoved",
"MessageRemoved"]:
bus.add_signal_receiver(removed,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["DisconnectReason", "Forwarded", "BarringActive"]:
bus.add_signal_receiver(value,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["IncomingBroadcast", "EmergencyBroadcast",
"IncomingMessage", "ImmediateMessage"]:
bus.add_signal_receiver(message,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["NotificationReceived", "RequestReceived"]:
bus.add_signal_receiver(ussd,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
mainloop = GLib.MainLoop()
mainloop.run()
Solution 1:[1]
Got it working by adding ,file=sys.stdout, flush=True to the print lines like below: print("{%s} [%s] %s" % (iface, path, member),file=sys.stdout, flush=True) Below is the complete python file with my edits.
#!/usr/bin/python3
from gi.repository import GLib
import dbus
import dbus.mainloop.glib
import sys
_dbus2py = {
dbus.String : str,
dbus.UInt32 : int,
dbus.Int32 : int,
dbus.Int16 : int,
dbus.UInt16 : int,
dbus.UInt64 : int,
dbus.Int64 : int,
dbus.Byte : int,
dbus.Boolean : bool,
dbus.ByteArray : str,
dbus.ObjectPath : str
}
def dbus2py(d):
t = type(d)
if t in _dbus2py:
return _dbus2py[t](d)
if t is dbus.Dictionary:
return dict([(dbus2py(k), dbus2py(v)) for k, v in d.items()])
if t is dbus.Array and d.signature == "y":
return "".join([chr(b) for b in d])
if t is dbus.Array or t is list:
return [dbus2py(v) for v in d]
if t is dbus.Struct or t is tuple:
return tuple([dbus2py(v) for v in d])
return d
def pretty(d):
d = dbus2py(d)
t = type(d)
if t in (dict, tuple, list) and len(d) > 0:
if t is dict:
d = ", ".join(["%s = %s" % (k, pretty(v))
for k, v in d.items()])
return "{ %s }" % d
d = " ".join([pretty(e) for e in d])
if t is tuple:
return "( %s )" % d
if t is str:
return "%s" % d
return str(d)
def property_changed(name, value, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s = %s" % (iface, path, name, pretty(value)),file=sys.stdout, flush=True)
def added(name, value, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s" % (iface, member, name, pretty(value)),file=sys.stdout, flush=True)
def removed(name, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s" % (iface, name, member),file=sys.stdout, flush=True)
def event(member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s" % (iface, path, member),file=sys.stdout, flush=True)
def message(msg, args, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s (%s)" % (iface, path, member,
msg, pretty(args)),file=sys.stdout, flush=True)
def ussd(msg, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s" % (iface, path, member, msg),file=sys.stdout, flush=True)
def value(value, member, path, interface):
iface = interface[interface.rfind(".") + 1:]
print("{%s} [%s] %s %s" % (iface, path, member, str(value)),file=sys.stdout, flush=True)
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(property_changed,
bus_name="org.ofono",
signal_name = "PropertyChanged",
path_keyword="path",
interface_keyword="interface")
for member in ["IncomingBarringInEffect",
"OutgoingBarringInEffect",
"NearMaximumWarning"]:
bus.add_signal_receiver(event,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["ModemAdded",
"ContextAdded",
"CallAdded",
"MessageAdded"]:
bus.add_signal_receiver(added,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["ModemRemoved",
"ContextRemoved",
"CallRemoved",
"MessageRemoved"]:
bus.add_signal_receiver(removed,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["DisconnectReason", "Forwarded", "BarringActive"]:
bus.add_signal_receiver(value,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["IncomingBroadcast", "EmergencyBroadcast",
"IncomingMessage", "ImmediateMessage"]:
bus.add_signal_receiver(message,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
for member in ["NotificationReceived", "RequestReceived"]:
bus.add_signal_receiver(ussd,
bus_name="org.ofono",
signal_name = member,
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
sys.stdout.flush()
mainloop = GLib.MainLoop()
mainloop.run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |