QProcess disable stdout redirection - qt

I want to start a process from a QT app and catch its termination event. Its done with the method QProcess::start(). But unlike in startDetached(), the standard output of the process is redirected to a buffer. I don't want that.
I can't find how to disable it. An ugly workaround is to call setStandardOutputFile("/dev/stdout")
test.h
#ifndef MY_TEST_H
#define MY_TEST_H
#include <QCoreApplication>
class MyApp : public QCoreApplication {
Q_OBJECT
private Q_SLOTS:
void subprocessStarted();
void subprocessFinished(int);
public:
MyApp( int & argc, char ** argv );
};
#endif
test.cpp
#include "test.h"
#include <QProcess>
#include <QCoreApplication>
#include <stdio.h>
#define ENTRY printf("%s\n", __PRETTY_FUNCTION__)
MyApp::MyApp( int & argc, char ** argv ) : QCoreApplication(argc, argv) {
ENTRY;
QProcess *process = new QProcess();
//process->setStandardOutputFile("/dev/stdout");
process->start("/bin/echo aaa");
bool b;
b = connect(process, SIGNAL(started()), SLOT(subprocessStarted()));
printf("connect started %d\n", b);
b = connect(process, SIGNAL(finished(int)), SLOT(subprocessFinished(int)));
printf("connect finished %d\n", b);
}
void MyApp::subprocessStarted() {
ENTRY;
}
void MyApp::subprocessFinished(int ret) {
ENTRY;
printf("%d\n", ret);
}
int main(int argc, char *argv[]) {
ENTRY;
MyApp a(argc, argv);
return a.exec();
}

Does QProcess::closeReadChannel(ProcessChannel channel) work for you?
Closes the read channel channel. After calling this function, QProcess
will no longer receive data on the channel. Any data that has already
been received is still available for reading. Call this function to
save memory, if you are not interested in the output of the process.
Like this-
QProcess *process = new QProcess();
process->start("/bin/echo aaa");
process->closeReadChannel(QProcess::StandardOutput);
process->closeReadChannel(QProcess::StandardError);

Related

QUdpSocket readReady not emitting signal

I'm just getting started with implementing a UDP client for my Qt application. I followed this example. I am able to send data to and receive data from a remote host. This has been verified at my remote host as well as the local client that I am developing this application for (verified locally by calling handleReadyRead() from my main.cpp). I can't seem to get the QUdpSocket to emit its readyRead signal. Why is that?
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTimer>
#include "udpclient.h"
static double value = 0;
static bool flash = true;
static UDPClient client;
int update1000()
{
QByteArray Data;
Data.append((char) 0x00);
Data.append((char) 0x2c);
Data.append((char) 0x1a);
Data.append((char) 0x2c);
Data.append((char) 0x92);
Data.append((char) 0xe6);
Data.append((char) 0xf6);
Data.append((char) 0xa0);
client.SendPacket(Data);
return 0;
}
int packetReady()
{
qInfo() << "packetReady";
return 0;
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<HorizontalBarGraph>("com.kubie.horizontalBarGraph", 1, 0, "HorizontalBarGraph");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QObject *object = engine.rootObjects()[0];
//qInfo() << "Initializing timer.";
QTimer timer1000;
QObject::connect(&timer1000, &QTimer::timeout, update1000);
timer1000.start(1000);
return app.exec();
}
udpclient.h
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include <QUdpSocket>
class UDPClient : public QObject
{
Q_OBJECT
public:
explicit UDPClient(QObject *parent = nullptr);
void SendPacket(const QByteArray &buffer);
private:
QUdpSocket *socket = nullptr;
signals:
public slots:
void handleReadyRead();
};
#endif // UDPCLIENT_H
udpclient.cpp
#include "udpclient.h"
UDPClient::UDPClient(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
//We need to bind the UDP socket to an address and a port
//socket->bind(QHostAddress::LocalHost,1234); //ex. Address localhost, port 1234
socket->bind(QHostAddress::AnyIPv4,6969); //ex. Address localhost, port 1234
//connect(socket,SIGNAL(readyRead()),this,SLOT(handleReadyRead()));
connect(socket, &QUdpSocket::readyRead, this, &UDPClient::handleReadyRead);
}
void UDPClient::SendPacket(const QByteArray &buffer)
{
socket->writeDatagram(buffer, QHostAddress("192.168.174.10"), 6969);
}
void UDPClient::handleReadyRead() //Read something
{
qInfo() << socket->hasPendingDatagrams();
QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
qInfo() << Buffer;
//The address will be sender.toString()
}
A QML application needs (at least) a QGuiApplication to run. You will get a debug output on the command line or in your IDE debug application output window about this. To get a more fatal warning for such kind of problems you can set the environment variable QT_FATAL_WARNINGS (see QDebug documentation for more information)

How to continually run QProcess while running my QML program?

The problem I have in my code is, once the QML engine uplouded the QProcess stop!! Is it possible to let QProcess run while the QMLis already in operation!
the idea is: I want the user to be able to interact with only one specific mouse and one keyboard and to continually check this condition!
Can some one check what is the problem here?
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
bool scanForDevices=0;
USBdeviceID *usbdeviceid =new USBdeviceID();
engine.rootContext()->setContextProperty("USBdeviceID", usbdeviceid);
QProcess OProcess;
QString Command; //Contains the command to be execute
Command = "lsusb";
while (1)
{
OProcess.start(Command,QIODevice::ReadOnly); //Starts execution of command
OProcess.waitForFinished(); //Waits for execution to complete
QString StdOut = OProcess.readAllStandardOutput(); //Reads standard output
QString StdError = OProcess.readAllStandardError(); //Reads standard error
cout<<"\n Printing the standard output..........\n";
cout<<endl<<StdOut.toStdString();
bool mouse1 = StdOut.contains("ID 046d:c03e");
bool keyBoard1 = StdOut.contains("ID 413c:1003");
if (mouse1 ==1 && keyBoard1==1)
{
// start main program
// revoke A signal to tell QML the correct devices are connected
usbdeviceid->setMouse1Detected(1);
usbdeviceid->setkeyBoard1Detected(1);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
}
}
}
It is not necessary to create infinite loops in Qt, instead the most elegant thing is to use the signals.
#include <QGuiApplication>
#include <QProcess>
#include <QQmlApplicationEngine>
class LSUSB: public QObject
{
Q_OBJECT
public:
LSUSB(const QStringList & ids, QObject *parent=nullptr): QObject(parent), status(false), ids(ids)
{
QString command = "lsusb";
connect(&process, &QProcess::readyReadStandardOutput, this, &LSUSB::onReadyReadStandardOutput);
connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &LSUSB::onFinished);
process.setProgram(command);
process.start();
}
Q_SIGNALS:
void isLoaded();
private Q_SLOTS:
void onReadyReadStandardOutput(){
QString stdout = process.readAllStandardOutput();
status = true;
for(const QString & id: ids){
status &= stdout.contains(id);
}
if(status){
process.kill();
Q_EMIT isLoaded();
}
}
void onFinished(){
if(!status)
process.start();
}
private:
QProcess process;
bool status;
QStringList ids;
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
LSUSB lsusb({"ID 046d:c03e", "ID 413c:1003"});
USBdeviceID usbdeviceid;
QObject::connect(&lsusb, &LSUSB::isLoaded, [&engine, &usbdeviceid](){
usbdeviceid.setMouse1Detected(1);
usbdeviceid.setkeyBoard1Detected(1);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
QCoreApplication::exit(-1);
});
return app.exec();
}
#include "main.moc"

dbus-send to QDBus program example

I try to send a message via dbus-send to this small example program.
But it is not received:
dbus-send --session --type=method_call / dbustester.test.slot_foo
The return code is 0 and not message is printed to the console.
Below is the source code.
main.cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtDBus/QtDBus>
#include <Example.h>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Example *e = new Example();
e->setupDBus();
return app.exec();
}
Example.h
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtDBus/QtDBus>
class Example : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "dbustester.test")
public:
Example(QObject* parent = NULL) :
QObject(parent)
{
}
void setupDBus()
{
QDBusConnection session = QDBusConnection::sessionBus();
if (!session.isConnected())
{
qFatal("Cannot connect to the D-Bus session bus.");
return;
}
session.connect("", "/", "dbustester.test", "slot_foo", this, SLOT(slot_foo(void)));
if(!session.registerObject("/", this, QDBusConnection::ExportScriptableContents)) {
qFatal("Cannot registerObject.");
return;
}
if(!session.registerService("dbustester.test")) {
qFatal("Cannot registerObject.");
return;
}
}
public slots:
Q_SCRIPTABLE void slot_foo()
{
qDebug() << "request received";
}
};
Build:
qmake -project
echo "CONFIG += qdbus" >> *.pro
qmake
I've found the answer while writing the question, but I wrote the question anyway. Some people might find it useful.
dbus-send --session --dest=dbustester.test --type=method_call / dbustester.test.slot_foo
I forgot the --dest argument. :>

how to send QList<Object *> objects to another class?

i'm trying to send a QList as a parameter to another class but for some reason i lose all it's content ...
(when i open the object with the debuger i see for objects...)
trying to send QList books to class Print:
class Store: public QWidget {
Q_OBJECT
public:
Analyze(QWidget *parent = 0);
void generate_report();
~Analyze();
private:
QList<Book *> books;
};
class Print
{
public:
Print();
bool generate_report_file(QList<Book *> *);
};
i'm sending books like this:
void Analyze::generate_report()
{
.
.
.
Print p;
if (!p.generate_report_file(&books))
QMessageBox::warning(this, "XML Escape","Error creating out.html", QMessageBox::Ok);
}
Small example
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QList>
#include <QString>
void print_list(QList<QString *> * k)
{
for (int i=0; i<k->size(); i++)
{
qDebug() << *k->at(i);
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<QString *> books;
books.append(new QString("asd"));
books.append(new QString("asdfgh"));
books.append(new QString("asdjhhhhhhtyut"));
print_list (&books);
return a.exec();
}
so just use * in function when calling elements of your QList, like in
qDebug() << *k->at(i); string
You should pass the QList by value. The reason, while this may seem silly on the surface, is that QList is implicitly shared. Please read http://doc.trolltech.com/latest/implicit-sharing.html to see more on that topic.
#include <QtCore/QtCore>
void
printList(const QStringList& list)
{
foreach (const QString& str, list) {
qDebug() << str;
}
}
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
QStringList list;
list << "A" << "B" << "C";
printList(list);
return QCoreApplication::exec();
}
There is already a class called QStringList to use. Also, you would want to pass it by reference. Also, you do not want to use pointers on containers or QString. As they are automatically implicitly shared. So it's bad design to use pointers on those two.

Qt QNetworkAccessManager does not emit signals

The function CheckSite() is called with an url like http://example.com, it initializes a QNetworkAccessManager object and connect() slots and signals.
The manger->get() call seems work (it generates http traffic) but does not call the slot replyFinished() at the request end.
What's wrong with this code?
#include <QtCore>
#include <QtNetwork>
class ClientHandler : public QObject
{
Q_OBJECT
QNetworkAccessManager *manager;
private slots:
void replyFinished(QNetworkReply *);
public:
void CheckSite(QString url);
};
void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
void ClientHandler::CheckSite(QString url) {
QUrl qrl(url);
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(qrl));
}
Nothing. I wrapped it so it was fully functional and it works fine:
// placed in client.cpp
#include <QtDebug>
#include <QCoreApplication>
/* YOUR CODE */
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
ClientHandler handler;
handler.CheckSite("www.google.com");
return app.exec();
}
#include "client.moc"
It output "DONE" as expected. Maybe the site you're checking really isn't returning? Maybe it needs authentication or is producing ssl errors?
What code do you have around that? Do you spin an event loop somewhere? e.g. qapp.exec() ?

Resources