using QProcess->setReadChannelMode(QProcess::MergedChannels) and using QProcess->readall() - c++

It's my first time to ask question on stackoverflow. And I'm a chinese girl, if my description about this problem has so much grammar error that you can't understand it easyly, I'm so sorry.
Below is my question:
headerfile:
class AdbDriver : public QObject
{
Q_OBJECT
private:
QString PnPutilPath_;
QProcess *process_;
public:
explicit AdbDriver(QObject *parent = 0);
~AdbDriver();
void installDriver();
};
AdbDriver::AdbDriver(QObject *parent):QObject(parent){
PnPutilPath_ = qgetenv("WINDIR") + "\\sysnative\\pnputil.exe";
process_ = new QProcess();
process_->setReadChannelMode(QProcess::MergedChannels);
process_->setStandardOutputFile("E:/log.txt");
}
sourcefile:
AdbDriver::~AdbDriver(){
delete process_;
}
void AdbDriver::installDriver(){
QFile file(PnPutilPath_);
if(file.exists()){
qDebug()<<"pnputil.exe exist";
QString generaladbDriver = "E:/driver_androidusb/generaladb.inf";
qDebug()<<"the programming include driver:"<<generaladbDriver;
QFile file(generaladbDriver);
if(file.exists()){
qDebug()<<"yes, the driver is right in bihu package";
}
else{
qDebug()<<"loss driver in bihu package";
}
QStringList arguments;
arguments<<"-i"<<"-a"<<generaladbDriver;
process_->start(PnPutilPath_, arguments);
while(!process_->waitForStarted()){
qDebug()<<"wait";
}
qDebug()<<"while out";
process_->waitForReadyRead();
qDebug()<<"start";
// qDebug()<<process_->readAll();
process_->close();
}
else{
qDebug()<<"sorry, your computer has no tool pnputil.exe.";
}
}
when i Commented out code
qDebug()<<process_->readAll();
and use
process_->setReadChannelMode(QProcess::MergedChannels);
process_->setStandardOutputFile("E:/log.txt");
it works properly.But if i use
qDebug()<<process_->readAll();
instead of
process_->setReadChannelMode(QProcess::MergedChannels);
process_->setStandardOutputFile("E:/log.txt");
it will be wrong. what's the reason?

According to Qt document, both setReadChannelMode and setStandardOutputFile have to be called before QProcess::start to take effect, so replacing
qDebug() << process_->readAll();
with
process_->setReadChannelMode(QProcess::MergedChannels);
process_->setStandardOutputFile("E:/log.txt");
is same as just commenting out qDebug() << process_->readAll();.
So I guess the child process does not output anything, so process_->readAll() will block, and the program halts.
(你看一下E:/log.txt有没有内容,估计是process_->readAll()阻塞了)

Related

How to link the execution of QProcess and the advancement of the QProgressBar for a very heavy computation loop

I have the following bash script to be executed on a GUI via QPushButton:
#!/bin/bash
rostopic echo -b test_LaserScan_PointCloud2_test2.bag -p /scan > test_landing_test_2.csv
rostopic echo -b test_LaserScan_PointCloud2_test2.bag -p /velodyne_points > vel_test_2.csv
The script will go through each file and extract the related .csv. The process is heavy and it takes a little bit. The problem is that there is no way to know how long it takes unless I put a QProgressBar, but I don't know how properly link the execution of the QProcess and the advancement of the QProgressBar correctly.
As of now the extraction happens successfully but the QProgressBar is not moving from 0.
Below I created a minimal verifiable example and for completeness the source code can be found here:
mainwindow.h
#include <QMainWindow>
#include <QProcess>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
Q_PROPERTY(bool running READ running NOTIFY runningChanged)
Q_PROPERTY(bool finished READ finished NOTIFY finishedChanged)
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
float progress();
bool running();
bool finished();
public Q_SLOTS:
void startComputation();
void finishComputation();
void updateProgress(int value);
signals:
void progressChanged();
void runningChanged();
void finishedChanged();
private slots:
void on_executeBtn_clicked();
private:
Ui::MainWindow *ui;
QProcess *executeBash;
bool m_running = false;
int m_progressValue = 0;
bool m_finished = false;
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->executeBash = new QProcess(this);
this->executeBash->setProcessChannelMode(QProcess::MergedChannels);
/*connect(this->executeBash, &QProcess::readyReadStandardOutput, [script = this->executeBash](){
qDebug() << "[EXEC] DATA: " << script->readAll();
});*/
connect(this->executeBash, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[script = this->executeBash](int exitCode, QProcess::ExitStatus exitStatus){
qDebug() << "[EXEC] FINISHED: " << exitCode << exitStatus;
if(script->bytesAvailable() > 0)qDebug() << "[EXEC] buffered DATA:" << script->readAll();
});
connect(this->executeBash, &QProcess::errorOccurred, [script = this->executeBash](QProcess::ProcessError error){
qDebug() << "[EXEC] error on execution: " << error << script->errorString();
});
connect(this->executeBash, &QProcess::readyReadStandardOutput, [this, script = this->executeBash](){
QString s = QString::fromUtf8(script->readAll());
qDebug() << "[EXEC] DATA: " << s;
auto match = QRegularExpression("Stage (\\d+)/(\\d+): (.*)").match(s);
if (match.hasMatch()) {
int x = match.captured(1).toInt();
int y = match.captured(2).toInt();
QString stage_info = match.captured(3);
qDebug() << "x = " << x;
qDebug() << "y = " << y;
qDebug() << "info = " << stage_info;
this->updateProgress(x * 100 / y);
}
});
// Initialization of the progressbar
m_running = false;
emit runningChanged();
m_finished = false;
emit finishedChanged();
updateProgress(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
// ProgressBag loading depending on the workload of the .sh file
float MainWindow::progress()
{
return m_progressValue;
}
bool MainWindow::running()
{
return m_running;
}
bool MainWindow::finished()
{
return m_finished;
}
void MainWindow::startComputation()
{
m_running = true;
emit runningChanged();
updateProgress(100);
}
void MainWindow::finishComputation()
{
m_finished = true;
emit finishedChanged();
m_running = false;
emit runningChanged();
}
void MainWindow::updateProgress(int value)
{
m_progressValue = value;
emit progressChanged();
if (m_progressValue == 100)
finishComputation();
ui->progressBarExecuteScript->setValue(value);
}
void MainWindow::on_executeBtn_clicked()
{
qDebug() << "Button clicked!";
this->executeBash->start(QStringLiteral("/bin/sh"), QStringList() << QStringLiteral("/home/emanuele/catkin_docking_ws/devel/lib/test.sh")); //will start new process without blocking
// right after the execution of the script the QProgressBar will start computing
//ui->progressBarExecuteScript->setValue(executeBash->readAll().toInt());
}
What I have done so far
1) After setting up all the necessary points I came across this source where the user had the same problem I have now but it was resolved via QDir, I am not sure about this solution though.
2) After going through the official documentation I found out about future watcher. I am new to this tool and am having difficulties understanding how to apply it and that is the reason why I created also the minimal verifiable example.
3) I tried to setValue of the progressbar based on the execution of the bash file. Or better its advancement as shown below:
ui->progressBarExecuteScript->setValue(executeBash->readAll().toInt());
I thought that this could have solved the problem but the QProgressBar stays at 0 value and I am not sure why.
4) I consulted the QProgressBar official documentation but I didn't find anything to help me solve the problem.
Please point to the right direction to solve this issue.
First, modify the script to output which stage it is currently doing:
#!/bin/bash
set -e
echo "Stage 1/2: Scan"
rostopic echo -b test_LaserScan_PointCloud2_test2.bag -p /scan > test_landing_test_2.csv
echo "Stage 2/2: Velodyne"
rostopic echo -b test_LaserScan_PointCloud2_test2.bag -p /velodyne_points > vel_test_2.csv
I also added set -e to make it stop on the first failed command.
Now, you can look for these "Stage x/y" markers in the output stream and call updateProgress if so.
connect(this->executeBash, &QProcess::readyReadStandardOutput, [this, script = this->executeBash](){
QString s = QString::fromUtf8(script->readAll());
auto match = QRegularExpression("Stage (\\d+)/(\\d+): (.*)").match(s);
if (match.hasMatch()) {
int x = match.captured(1).toInt();
int y = match.captured(2).toInt();
QString stage_info = match.captured(3);
this->updateProgress(x * 100 / y);
}
});

reading rows and columns from a textfile QT GUI C++

I want to read the size and the values of my matrix from a text file.
an example of a text file
graphe.txt
4 (the size of the matrix)
1 0 1 0
1 1 1 1
0 1 1 1
0 0 0 1
I tried a code but unfortunately it didn't work .I got this errors:
error: 'class MainWindow' has no member named 'display' this->display->setText(val);
error: cannot convert 'QString' to 'int' in assignment
matrice[ligne][i]=val;
void MainWindow::remplir_matrice(int taille_mat,int matrice[][50] )
{
QFile file("/home/yosra/degré/degré/graphe.txt");
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
int i=1;
int ligne=1;
while ((!in.atEnd())&&(ligne<=taille_mat))
{
ligne++;
QString line = in.readLine();
QStringList list = line.split(" ");
QString val = list.at(i);
this->display->setText(val);
val.toInt();
matrice[ligne][i]=val;
i++;
}
file.close();
}
}
void MainWindow::afficher(int matrice[][50],int taille_mat)
{
qDebug()<<" les elements de matrice";
for(int i=0;i<taille_mat;i++)
{
for(int j=0;j<taille_mat;j++)
qDebug()<<"M "<<matrice[i][j]<<endl;
}
}
void MainWindow::parourir_fichier(int matrice[50][50],int taille_mat)
{
QFile file("/home/y/degré/classement/graphe.txt");
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
QStringList list;
QString line = in.readLine();
QString val = list.at(0);
this->display->setText(val);
val.toInt();
taille_mat=val;
qDebug() << "taille_mat=" << taille_mat<<endl;
file.close();
}
remplir_matrice(taille_mat,matrice);
afficher(matrice,taille_mat);
}
this is my MainWindow's header
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void remplir_matrice(int taille_mat,int matrice[][50] );
void parourir_fichier(int matrice[][50],int taille_mat);
void afficher(int matrice[][50],int taille_mat);
private:
Ui::MainWindow *ui;
int matrice[50][50];
int taille_mat;
};
Could it be that you mean:
ui->display->setText(val);
The MainWindow class does not have a pointer to the display object. Perhaps the display object was created with Qt Creator editor as a TextEdit field?
Update
If you just want to see a value while you are still developing your code, you are probably better off using qDebug() (documentation here). You will need to include to make this work. The output will be shown in the output pane when you run the application from Qt Creator.
#include <QDebug>
// ...further down in your code:
qDebug() << "Output of val:" << val;
The second error message is pretty clear, isn't it? A QString cannot be automatically converted to an int. I don't really know Qt, but a quick Google search reveals the existence of a toInt member function, so the following probably works:
matrice[ligne][i]=val.toInt();
As for the first error message, this->display supposes the existence of a member variable in MainWindow. If display is a member function (it certainly sounds like one), then you need parentheses: this->display(). If there is no member function of that name either, then we cannot help you much with the code that you have posted.

QSqlDatbase is closed before the query finishes

I am trying to implement a database object for my application, but it became a nightmare when I started using multiple connections. Below, you can see my database C++ class code:
// here are the declarations
QString server_addr;
QString username;
QString password;
QString database_name;
QSqlDatabase connection;
QString error;
QString connectionName;
QSqlQuery m_query;
// and here are the definitions
database::database(QString connectionName) {
preferences p; p.read();
QString iConnectionName = (connectionName == "") ? default_connection_name : connectionName;
this->connectionName = iConnectionName;
if (QSqlDatabase::contains(iConnectionName))
this->connection = QSqlDatabase::database(iConnectionName);
else this->connection = QSqlDatabase::addDatabase("QMYSQL", iConnectionName);
this->connection.setHostName(p.database->server_addr);
this->connection.setUserName(p.database->username);
this->connection.setPassword(p.database->password);
this->connection.setDatabaseName(p.database->database_name);
this->connection.setPort(p.database->serverPort);
if (!connection.open())
{
this->error = this->connection.lastError().text();
}
else this->error = "";
}
QSqlQuery database::query(QString query_text) {
this->m_query = QSqlQuery(query_text, this->connection);
this->m_query.exec();
return m_query;
}
database::~database() {
if (!this->m_query.isActive()) QSqlDatabase::removeDatabase(this->connectionName);
qDebug() << "database object destroyed\n";
}
The problem occurs in a another class (that uses the database):
databaseAdaptor::databaseAdaptor() {
this->db = database();
// other construction operations
}
void databaseAdaptor::fetch() {
QSqlQuery q = db.query("SELECT * FROM `activities`");
qDebug() << "Rows count: " << q.numRowsAffected();
}
It worked some versions in the past, but for some reason, now the output from qDebug() is
Rows count: -1 // and it should be 2 for my current version of database.
In the Qt Documentation, it is said that this class' members are thread-safe. Could this be the problem? I mean, could the thread end before the queries finish their execution?
If so, how can I keep the connection open until all the queries finished their execution?
(I know it's much to read, but I am sure that other might have this problem at some point, so please take your time and read it). Thank you!

Memory leak with post requests and QNetworkAccessManager

I'm making a program that uses lots of timer and, at intervals of 4 seconds, does an online post to a php script.
I'm coding in QtCreator 5.1. I use classes just like the ones below.
The one below just populates a task list, but throughout the course of 8 to 12 hours, the memory that the program takes up just keep rising and rising gradually.
What am I doing wrong while using this class?
I have to be able to keep posting online like I already am, about every 4 to 8 seconds.
Here's a simple class for taking care of one of my processes:
Header file: tasklistprocess.h
#ifndef TASKLISTPROCESS_H
#define TASKLISTPROCESS_H
#include <QThread>
#include <QtCore>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QListWidget>
#include <QTabWidget>
#include "globalhelper.h"
#include "securityinfo.h"
class TaskListProcess : public QThread
{
Q_OBJECT
public:
explicit TaskListProcess(QListWidget *obj_main, QTabWidget *tabs_main, QString user, QObject *parent = 0);
signals:
void upTaskStorage(int key,QHash<QString,QString> item);
private:
GlobalHelper gh;
Securityinfo sci;
QNetworkAccessManager *nam;
QNetworkRequest request;
QByteArray data;
// this is the disposable params for reusage through out the class
QUrlQuery params;
QString post_data;
QString user_name;
QTimer *tasklist_tmr;
bool get_task_list;
QListWidget *obj;
QTabWidget *tabs;
private slots:
void setTaskList();
void replyFinished(QNetworkReply *reply);
void sendPost(QString file_name, QUrlQuery params);
};
#endif // TASKLISTPROCESS_H`
Source file: tasklistprocess.cpp
#include "tasklistprocess.h"
TaskListProcess::TaskListProcess(QListWidget *obj_main, QTabWidget *tabs_main, QString user, QObject *parent) :
QThread(parent)
{
user_name = user;
get_task_list = false;
obj = obj_main;
tabs = tabs_main;
tasklist_tmr = new QTimer(this);
connect(this,SIGNAL(started()),this,SLOT(setTaskList()));
connect(tasklist_tmr,SIGNAL(timeout()),this,SLOT(setTaskList()));
nam = new QNetworkAccessManager(this);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
request.setRawHeader( "User-Agent" , "Mozilla Firefox" );
// here we connect up the data stream and data reply signals
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
void TaskListProcess::setTaskList()
{
qDebug() << "Your task list was set";
bool in = false;
if(!(tasklist_tmr->isActive()))
{
tasklist_tmr->start(10000);
in = true;
}
if(!(get_task_list))
{
params.clear();
params.addQueryItem("user_name", user_name);
params.addQueryItem("logged_in", "1");
sendPost("getTaskList.php",params);
get_task_list = true;
}
else
{
if(post_data.contains("|*|"))
{
//here i retrieve a piece of information from a php script which is stored in a custom string format
// here we clear the list for the new data to be put in
if(obj->count()>0)
{
obj->clear();
}
int key = 0;
foreach(QString inner_task,post_data.split("|*|"))
{
QHash<QString,QString> task_cont;
//qDebug() << " ";
if(inner_task.contains("*,*"))
{
foreach(QString task_val,inner_task.split("*,*"))
{
if(task_val.contains("*=*"))
{
QStringList key_pairs = task_val.split("*=*");
task_cont.insert(key_pairs[0],key_pairs[1]);
if(key_pairs[0] == "tt")
{
QString val_in;
if(key_pairs[1].length()>10)
{
// this sets the title to the shortened version
// if the string length is too long
val_in = key_pairs[1].left(10) + "....";
}
else
{
val_in = key_pairs[1];
}
obj->addItem("Task :" + QString::fromUtf8(key_pairs[1].toStdString().c_str()));
}
}
}
}
//task_storage.insert(key,task_cont);
emit upTaskStorage(key,task_cont);
key ++;
}
}
get_task_list = false;
}
// here we're checking to see if they are looking at the task tab so it doesn't keep changing
// back and forth between the tabs
bool change = true;
if(tabs->currentIndex() != 0)
{
change = false;
}
if(change)
{
tabs->setCurrentIndex(0);
}else if(in)
{
tabs->setCurrentIndex(0);
}
}
void TaskListProcess::replyFinished(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
return;
}
QString data = reply->readAll().trimmed();
post_data = data;
if(get_task_list)
{
setTaskList();
}
}
void TaskListProcess::sendPost(QString file_name, QUrlQuery params)
{
post_data = "";
QUrl url(sci.getHost() + file_name);
url.setQuery(params);
data.clear();
data.append(params.toString().toUtf8());
request.setUrl(url);
nam->post(request, data);
}
From the Qt docs http://qt-project.org/doc/qt-5.1/qtnetwork/qnetworkaccessmanager.html
Note: After the request has finished, it is the responsibility of the
user to delete the QNetworkReply object at an appropriate time. Do not
directly delete it inside the slot connected to finished(). You can
use the deleteLater() function.
I would suggest calling reply->deleteLater() in your replyFinished() method.
You should call deleteLater() for an QNetworkReply object after use.
Note: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished(). You can use the deleteLater() function.
More information here: http://harmattan-dev.nokia.com/docs/library/html/qt4/qnetworkaccessmanager.html
Thank you everyone for the hel.
It was very simply the "deleteLater()" on the reply in the
void replyFinished(QNetworkReply *reply)
{
}
and it should have looked like this
void replyFinished(QNetworkReply *reply)
{
// after all of your processing
reply->deleteLater();
}
it was such a small problem but one that drove me crazy for a long time so i hope this helps

strange behavior with exception (some method not called is executed)

I've been struggling with a really strange behavior that I do not manage to resolve on my own, so here I am.
I'm creating an GUI for a console application which works great. The user can either select a file to be loaded or reload a previous selected file. Those two actions are handled with two different slots. I'm handling errors in the file's format with exceptions and it worked great in the console version, but for the GUI not too well for the moment...
When an error is thrown in the openFile slot, the catch block makes his work and the method is stopped as expected BUT then the other slot is being called unexpectedly. I have no idea why and so have no idea how to correct that behavior.
Here's the relevant code :
void Loader::openSourceFile()
{
fileName = QFileDialog::getOpenFileName(myWindow, tr("Select source file"), QString(), tr("Text files (*.txt)"));
try{
parseSourceFile();
} catch(MyException &e)
{
QString msg = QString(e.what());
myWindow->alertUser(msg);
return;
}
}
void Loader::reloadSourceFile()
{
try{
parseSourceFile();
} catch(MyException &e)
{
QString msg = QString(e.what());
myWindow->alertUser(msg);
return;
}
}
the context of use :
myLoader = new Loader(this);
menuTop = menuBar()->addMenu(tr("&File"));
//open source file
openAction = new QAction(QIcon(":images/document.png"), tr("&Open"), this);
connect(openAction, SIGNAL(triggered()), myLoader, SLOT(openSourceFile()));
menuTop->addAction(openAction);
//reload source file
reloadAction = new QAction(QIcon(":images/reload.png"), tr("&Reload"), this);
connect(openAction, SIGNAL(triggered()), myLoader, SLOT(reloadSourceFile()));
menuTop->addAction(reloadAction);
Note : after heavy debugging, I found that one function in the moc file of that class is being called after the execution of the catch block :
void Loader::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
Q_ASSERT(staticMetaObject.cast(_o));
Loader *_t = static_cast<Loader *>(_o);
switch (_id) {
case 0: _t->openSourceFile(); break;
case 1: _t->reloadSourceFile(); break;
default: ;
}
}
Q_UNUSED(_a);
}
it is called with _id = 1 which explain the undesired execution of the other slot. But why this happens ??? Does anybody could explain how this can be avoided ? I've already made my own application class derived from qApplication and override notify() but that does not change anything.
Should:
reloadAction = new QAction(QIcon(":images/reload.png"), tr("&Reload"), this);
connect(openAction, SIGNAL(triggered()), myLoader, SLOT(reloadSourceFile()));
menuTop->addAction(reloadAction);
be:
reloadAction = new QAction(QIcon(":images/reload.png"), tr("&Reload"), this);
connect(reloadAction, SIGNAL(triggered()), myLoader, SLOT(reloadSourceFile()));
// ^^^^^^^^^^^^
menuTop->addAction(reloadAction);