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
...
Related
I'm looking for the way to show up usb storage path when they are plugged in, the path must be shown in a combobox (in a gui that I'm designing with qt creator (qt 5.9)). I have been searching how to do it but I have not found anything. What I want it's something like:
https://catenarios2.files.wordpress.com/2012/11/002.jpg
Could you please help me to carry on my project? I would be very grateful if you provide an example.
Thank you a lot
The basic idea is the same -- you launch Linux tool via QProcess and parse the result. Here is a simple sketch:
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <usb.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess devList;
devList.start("lsblk", QStringList() << "-o" << "KNAME");
if (!devList.waitForStarted())
return false;
if (!devList.waitForFinished())
return false;
QString result = QString(devList.readAll());
qDebug() << result;
return a.exec();
}
You can use any other siutable command (quite easy to find them) and should improve parsing, of course, but generally it's all the same.
AFAIK, mount points could be obtained from /proc/mounts with something like...
#include <QCoreApplication>
#include <mntent.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct mntent *ent;
FILE *aFile;
aFile = setmntent("/proc/mounts", "r");
if (aFile == NULL) {
perror("setmntent");
exit(1);
}
while (NULL != (ent = getmntent(aFile))) {
printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
}
endmntent(aFile);
return a.exec();
}
Better than cat launching or someting else, also taken from some snippet and should be improved.
And, finally, in case you'll need USD device info, it could be something like...
#include <QCoreApplication>
#include <usb.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
printf("Trying device %s/%s\n", bus->dirname, dev->filename);
printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);
}
}
return a.exec();
}
This needs sudo apt-get libusb-dev + compiling with -lusb.
Not really much of Qt in the problem and more fundamental "coding" solutions possible, but hopefully that'll give you a push towards appropriate solution.
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();
}
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
I'm trying to do something very simple, I just want to print my native language, pt-br in Windows Console.
IDE Creator
I created a new project->other->Qt Console Application the I put it in my main.cpp file:
#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "aeiou áéíóú";
std::cout << "aeiou áéíóú" << endl;
return 0;
}
here is what I got:
C:\Users\maiko.costa\testeQtConsole\debug>testeQtConsole.exe
aeiou ßÚݾ·
aeiou ßÚݾ·
C:\Users\maiko.costa\testeQtConsole\debug>
I've tried it too, but with the same previous output:
#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec *codec = QTextCodec::codecForName("CP1252");
QTextCodec::setCodecForCStrings(codec);
qDebug() << "aeiou áéíóú";
std::cout << "aeiou áéíóú" << endl;
return 0;
}
The System encode for Windows 7 is it right ?
what am I missing ?
I am not that familiar with QT but I think this can help you just as well. The Windows console uses the OEM char set. Therefore, in order to properly print characters on std::cout they need to be encoded using OEM. This can be accomplished using the Windows API CharToOem.
Small example, just so you get the idea (here input is assumed to be UTF16):
void oemPrint(const wchar_t* str) {
char* chars = (char*)alloca(strlen(str)+1);
CharToOemW(str, chars);
fputs(chars, stdout);
}
// Usage:
oemPrint(L"aeiou áéíóú");
EDIT: A QT solution might be to use QTextCodec::codecForName("IBM 850") - this is the OEM codec.
I find the solution in this thread. Output unicode strings in Windows console app
If I ran chcp 65001 in windows console before I ran my app the characters are printed correctly.
I don't know how to workaround it in my source code, then I call this program manually with the start cpp function.
Here is the return line of function I wrote that displays passwords as ● ● ● ● ●
return QString::fromUtf8( "\u25CF \u25CF \u25CF \u25CF \u25CF" );
QString::fromUnicode should work the same.
Maybe something like:
QString x = QString::fromUtf8( "\u25CF \u25CF \u25CF \u25CF \u25CF" );
std::cout << qPrintable(x) << std::endl;
Of course change it to QString::fromUnicode... hope this helps
QString a="aeiou áéíóú";
std::cout<< a.toStdString().data();