I'm trying to get Qt creator to print a user input by using a push button on an UI into the terminal. As of now, the code is executable on the terminal via human input. Here is the code:
void MainWindow::on_pushButton_clicked()
{
QProcess::execute("/catkin_ws/devel/lib/submodbus");
system("cd catkin_ws/devel/lib/submodbus");
system("./submodbus_node");
}
Current output when using the code
Output via human input
The versions i'm running on are:
-Ubuntu 16.04
-QT Creator 3.5.1
system can't change the current directory globally. but could use like this:
system("cd /catkin_ws/devel/lib/submodbus && ./submodbus_node");
or using QProcess::setProgram with QProcess::setWorkingDirectory
QProcess p;
p.setProgram("submodbus_node");
//p.setArguments(QStringList()<<args); // if you need
p.setWorkingDirectory("/catkin_ws/devel/lib/submodbus");
p.start();
or QDir::setCurrent
QDir::setCurrent("/catkin_ws/devel/lib/submodbus");
QProcess::startDetached("submodbus_node");
Test demo, create three files in the parent directory:
#include <QApplication>
#include <QProcess>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
system("cd ../ && touch test1.txt");
QProcess p;
p.setProgram("touch");
p.setArguments(QStringList()<<"test2.txt");
p.setWorkingDirectory("../");
p.start();
QDir::setCurrent("../");
QProcess::startDetached("touch test3.txt");
return a.exec();
}
Related
I'm printing output from QProcess::readAllStandardOutput() (on Ubuntu 18.04) and it works otherwise fine, but \n characters are not actually line feeds and somehow appear literally as a part of the string:
/usr/local/lib/libpcl_search.so\n/usr/local/lib/libpcl_sample_consensus.so\n/usr/local/lib/libpcl_io.so\n/usr/local/lib/libpcl_segmentation.so\n/usr/local/lib/libpcl_common.so\n/usr/local/lib/libboost_random.so\n/usr/local/lib/libboost_math_tr1l.so
That was output when running find / -name "*so" command with QProcess printed like this:
qDebug() << m_process->readAllStandardOutput();
I guess this is an encoding issue..?
the problem is caused because QDebug is going to show the endlines and similar characters because you are passing them a QByteArray, if you want to see the output you want then use qPrintable:
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess process;
QObject::connect(&process, &QProcess::readyReadStandardOutput, [&process](){
qDebug()<< qPrintable(process.readAllStandardOutput());
});
process.start("find / -name \"*so\"");
return a.exec();
}
Output:
/snap/core/4917/lib/crda/libreg.so
/snap/core/4917/lib/i386-linux-gnu/ld-2.23.so
/snap/core/4917/lib/i386-linux-gnu/libBrokenLocale-2.23.so
/snap/core/4917/lib/i386-linux-gnu/libSegFault.so
/snap/core/4917/lib/i386-linux-gnu/libanl-2.23.so
/snap/core/4917/lib/i386-linux-gnu/libc-2.23.so
/snap/core/4917/lib/i386-linux-gnu/libcidn-2.23.so
...
I have been trying to create a directory in root directory of Linux. But as I am not much familiar with Linux platform I am unable to write the correct program in QT. Can you please have a look at my code and tell me where did I did mistake?
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QString>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir mDir;
QString mpath="/home/qtfile";
if (!mDir.exists(mpath))
{
mDir.mkpath(mpath);
qDebug() <<"Created";
}
else if (mDir.exists(mpath))
{
qDebug() <<"Already existed";
}
else
{
qDebug()<<"Directory could not be created";
}
return a.exec();
}
Thank you for your time and consideration
EDIT:- Thank you everyone. Now this problem is solved
This might be the issue of access rights #SamratLuitel is writing about in the comments.
Hence, you could try to give it a go in the proper home location, for example:
const QString& homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QDir dir(homePath);
if (dir.mkdir("somedir"))
{
//success
}
I have a simple Qt programm which start's another program with QProcess::execute().
It works under Windows (MSVC 32/64, Mingw-32), Linux (GCC), MAC (CLang).
But if I try to run it in a bash under Windows (Bash on ubuntu on windows) the call to QProcess::execute() never returns.
I've also tried to use QProcess::start() but neither the QProcess::started nor the QProcess::errorOccured signal is issued, and QProcess::start() also never returns.
Is there some incompatibilty with this configuration?
The programm was compiled with GCC 4.8.4 and Qt 5.7 for Linux
The programm looks like this:
#include <QProcess>
#include <QCoreApplication>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv)
QStringList args = app.arguments().mid(1);
int retcode = QProcess::execute("programm", args);
if (retcode == 0)
{
// do something here
}
return retcode;
}
"programm" is an executable located some where in the search path.
Hi I am firing a detached process from Qt using QProcess. I want to read the console output of the process in a QString. Here is the code
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess proc;
proc.startDetached("C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",
QStringList() << "/c" << "c:\\Qt\\Qt5.3.0\\Tools\\QtCreator\\bin\\tryScript\\firstBatch.bat");
proc.waitForFinished();
qDebug() << proc.readAllStandardOutput();
return a.exec();
}
QProcess::startDetached is not a member function, its a static function, so
proc.startDetached(...)
is equivalent to :
QProcess::startDetached(...)
Hence there's no state or output in your proc variable for the detached process. Use the start() method if you want to start the process as a subprocess of your application and read its output.
I am updating the printer list using availablePrinters(). But it fails to list the new printer added while running application. It is working fine with Qt 4.
The code can be seen below:
#include <QCoreApplication>
#include <QtPrintSupport/QPrinterInfo>
#include <QThread>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (1) {
QThread::msleep(3000);
qDebug()<<"List of printers";
QList<QPrinterInfo> printerList=QPrinterInfo::availablePrinters();
foreach (QPrinterInfo printerInfo, printerList) {
qDebug()<<printerInfo.printerName();
}
}
return a.exec();
}
That was a bug with the existing Qt version, and It got fixed on the next version