Any example of how to integrate Qt in some ROS package? - qt

I would like to use an QT as an user interface for my mobile platform. I have a sensor package consist of IMU , Laser, Kinect and wheel encoders. Would like to have an input button that run a rose node than do something with the data and gives an output results. I was told that should set up like any other Qt + CMake project.
I have used the simple example
#include "QDebug"
#include "ros/ros.h"
int main(int argc, char** argv)
{
ros::init(argc, argv, "Qt_test");
qDebug()<<"Hello world";
return 0;
}
Than successfuly made the CmakeList file. I could compile it with make. But when I tried to run it with `./test_qt` I got this error
bash: ./test_qt: No such file or directory
An help
Thanks

You must use some package like this http://wiki.ros.org/qt_tutorials?distro=hydro.
And using cmake for build your project. In the case who you want use qtcreator you must call the ide by a command-line. But before, your bash must know the path of ROS installation.
1 - type that in your bash: source devel.setup.bash
2 - create your qtRos node/package in your catkin workspace.
3 - call qtcreator in command line

Related

Qprocess and avrdude

i'm trying to create a simple QT program that allows me to launch avrdude without using command line operations.
I saw that with Qprocess it's easy to launch any kind of program and I tried succesfully with simple programs like Explorer Word and Others.
The problem is that when i try to open cmd.exe nothing happens, even if i try to pass a batch file containing all the information to launch correctly avrdude.
Here's the code
QProcess *process = new QProcess(this);
process->startDetached("cmd.exe",QStringList()<<"C:/avrdude/avr.bat");
I wrote a minimal sample application which shows how to start cmd with a command using QProcess::startDetached() (on button click):
// standard C++ header:
#include <iostream>
// Qt header:
#include <QApplication>
#include <QMainWindow>
#include <QPushButton>
#include <QProcess>
int main(int argc, char **argv)
{
// main application
#undef qApp // undef macro qApp out of the way
QApplication qApp(argc, argv);
QMainWindow qWin;
QPushButton qBtn(QString::fromLatin1("Start cmd"));
QObject::connect(&qBtn, &QPushButton::clicked,
[](bool) {
bool ret = QProcess::startDetached(
#if 1 // let Windows search for cmd.exe in %PATH%
QString::fromLatin1("cmd.exe"),
#else // define exact path of cmd.exe
QString::fromLatin1("C:\\Windows\\system32\\cmd.exe"),
#endif // 1
QStringList()
<< QString::fromLatin1("/K")
<< QString::fromLatin1("echo Hello"));
std::cout << "QProcess::startDetached():"
<< (ret ? "OK." : "Failed!") << std::endl;
});
qWin.setCentralWidget(&qBtn);
qWin.show();
return qApp.exec();
}
The Qt project file is left as exercise. (Sorry, I used CMake for this.)
Please, note the #if 1. If 1 is replaced by 0 the alternative code with full path is used. (During chat session we examined special problems with starting the cmd.exe.) On my system, both alternatives did as well.
(My system: Windows 10, VS2013, Qt 5.7)
I too have been working on a Qt program where there are a couple calls to AVRDUDE. This is what worked for me. Here's the code I made for a read of the AVR device through AVRDUDE, followed by a couple of comments.
void MainWindow::call_AVRDUDE_read() //AVR READ
{
QProcess CommandPrompt;
QStringList Arguments;
QString COMPortUsed = (ui->COM_Port_Used->text()); // get the COM port from the user off UI
Arguments << "/C avrdude -c arduino -P "+ COMPortUsed +" -b 115200 -p ATmega328P -e -U eeprom:r:fromEEPROM.bin:r";
CommandPrompt.start("cmd",Arguments);
CommandPrompt.waitForFinished();
}
Here's something else which may well influence things in your application. In my case, I am reading the AVR's EEPROM. There is another routine that writes the EEPROM, but it is essentially the same as above, but a different script is sent.
In BOTH these cases, the AVRDUDE operation takes a few seconds to perform its task. When you use the QProcess::startDetached(), it has the disadvantage that control will return IMMEDIATELY after the AVRDUDE script is called through the QProcess. This can cause problems, if for instance you wanted to (as in my case) read the contents of the EEPROM and try to do so before the read actually completes.
An alternative to startDetached() you might consider trying is shown below. This will retain control until the process is finished, which may be pretty important to you. Use these two lines to replace the startDetached() call you are currently using.
CommandPrompt.start("cmd",Arguments);
CommandPrompt.waitForFinished();
This will wait for the AVRDUDE process to finish before control is returned.
The take away here though is that QProces::startDetached() may return prematurely in your application. Just beware of that.

QSqlDatabase hello world application fails to debug

I'm attempting a QSqlDatabase hello world application using PostgreSQL. My environment is as follows: Windows 7 64-bit, Qt 4.8.2, PostgreSQL 9.0.13. The following code compiles, but will not debug, i.e. when I place a break on the QSqlDatabase::drivers() line, but the code exits with an exception. The application runs as expected when I comment out this line. Any suggestions?
#include <QApplication>
#include <QMainWindow>
#include <QtSql/QtSql>
#include <QtSql/QSqlDatabase>
int main(int argc, char *argv[])
{
QApplication prog(argc, argv);
QMainWindow *mainWin = new QMainWindow;
QStringList drvlst = QSqlDatabase::drivers(); // <-- problem!
mainWin->show();
return prog.exec();
}
Another example of DLL hell and how Dependency Walker can be a great tool for diagnosing potential bugs of this nature. Again, in this case, the linker was traversing the PATH environment variable to find QTSQLD4.DLL library dependencies. The linker found the openVPN SSLEAY32.DLL (which is actually 64 bit) instead of the postgreSQL version of SSLEAY32.DLL. Moving the openVPN url to the end of the PATH environment variable resolved this problem.

How to get executable name in Qt

I run a Qt application, what I want to know is this running binary file name.
I must (partially) disagree with the other comments that it is not a Qt question: There is a Qt method QCoreApplication::applicationFilePath() which gives the directory+filename of the executable.
On Linux this will try to use /proc, and on Windows perhaps GetModuleFileName(). According to the docs it will fall back to argv[0].
You could then use QFileInfo to split it into an executable name and a directory.
QFileInfo(QCoreApplication::applicationFilePath()).fileName()
The Qapplication parses the commandline arguemnts, the first entry is the name of the executable - this is roughly the same as argv[0] in standard C but has a few extra complexities on windows if you have a Unicode build or if the application is started as a service
See http://doc.qt.io/qt-5/qcoreapplication.html#arguments
Again not really a Qt question. To find the name of the binary file executed it would be something like.
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
cout << argv[0] << endl;
return 0;
}

Why is Qt reporting > 14000 errors in this one page source file?

Here is the code:
#include <QtCore/QCoreApplication>
#include <QProcess>
#include <QProcessEnvironment>
int main(int argc, char *argv[])
{
QProcessEnvironment env;
// Environment variables required by CGI apps
env.insert("QUERY_STRING", url.encodedQuery());
env.insert("REQUEST_URI", url.toString());
env.insert("REQUEST_METHOD", "GET");
env.insert("REMOTE_ADDR", pSocket->peerAddress().toString());
//==========
QProcess process;
process.setProcessEnvironment(env);
process.start("php-cgi",QProcess::ReadWrite);
process.write("<?php print_r($_GET); ?>");
process.closeWriteChannel();
process.waitForFinished();
qDebug(process.readAll().data());
return 0;
}
Qt Creator reports more than 14000 errors. Here is a small sample of them:
In file included from ../QProcess/main.cpp:2:
../QProcess/QProcess:1: error: stray ‘\177’ in program
../QProcess/QProcess:1: error: stray ‘\2’ in program
../QProcess/QProcess:1: error: stray ‘\1’ in program
../QProcess/QProcess:1: error: stray ‘\1’ in program
In file included from ../QProcess/main.cpp:2:
../QProcess/QProcess:1:8: warning: null character(s) ignored
../QProcess/QProcess:1: error: stray ‘\2’ in program
Here is a simple example instance of this error. The contents of a file called new.cpp:
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Brand new." << std::endl;
return 0;
}
Now compile it:
ppu32-g++ new.cpp -o new
No problem. Let's compile it again:
ppu32-g++ new.cpp -o new
OMG! Screen after screen of errors! Most of these errors look like this:
./new:1: error: stray '\177' in program
The problem is that the compiler is trying to use the new file in the current directory, as the C++ standard library <new>; used by <iostream>. In fact any file in the current directory, named after a C++ standard library header, which is used in the code being compiled, will likely cause an error. That our problem file is an executable obfuscates the error messages.
It doesn't occur on all the gcc machines I've tried. ppu32-g++ 4.1.1 on PS3 Cell Fedora Linux certainly chokes.
If that problem arises not on every host then probably difference in the compiler. Difference is likely in the header files directory scan order.
Such error should not arise if standard headers directory scanned before others.
As a workaround its enough to remove project target file manually befor building target. But this is not a programmers way (which are very lazy creatures).
Programmers way - is to learn QMake to delete target file just before project sources compilations (but under this way link phase will be done permanently). For me (QMake 2.01a under Linux) this is achived by addition of following lines into project file:
deltarget.commands = $$QMAKE_DEL_FILE $$TARGET
QMAKE_EXTRA_TARGETS += deltarget
PRE_TARGETDEPS += deltarget
This declares target deltarget which becomes built before current project.
This sample uses undeclared in QMake's manual variable QMAKE_DEL_FILE which used to remove files (its content of course platfrom-dependent).

How to get command line arguments for a running process

In my program, I have been receiving an error when I use a
command-line compile command for mxmlc. The error is related to an
embedded font name not being correctly identified by flex in the
system fonts list.
However, on a whim, I decided to copy the code to Flex Builder and
compile it there. To my surprise, it worked, and it found the proper
font using the same system name I had given (PMingLiU).
I suspected my problem may be a locale one, and that my system cannot
correctly identify the font name because of locale considerations.
I've tried setting the locale of the compile code to en_US, to no
avail. So I would like to ask if anyone here knows how exactly Flex Builder invokes the MXML compiler and what differences there are compared to running mxmlc directly? We know it's not using the mxmlc.exe directly, since we tried replacing mxmlc with our own executable to capture the command line parameters.
If it matters, the OS used is Windows XP.
Although I don't have the exact answer to your question (what command line arguments Flex Builder passes to mxmlc.exe), I do have a meta-answer for you. You can find the command line by using one of two methods.
The first is platform-agnostic but will require you to compile a small C++ program. I've used this approach before when solving similar problems. What you can do is create a wrapper application which simply outputs the command line to a file. Build this application and drop it in as a temporary replacement for your mxmlc.exe, and when Flex Builder executes it you'll be able to access the resulting file "cmdline.txt" to get the full command line that it was called with:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ofstream cmdLine;
cmdLine.open("cmdline.txt");
for (int i = 0; i < argc; i++) {
cmdLine << argv[i];
if (i < argc)
cmdLine << " ";
}
cmdLine.close();
return 0;
}
If you don't feel right about playing this dirty trick on Flex Builder, there is an alternative assuming you're running on Windows. You can use WMI to iterate over all of the running processes and grab their command line information. Ruby being my language of choice, this would require you to install the Ruby interpreter for Windows which you can do easily with the One-Click Ruby Installer for Windows.
After installing, just run this script as soon as Flex Builder kicks off your build:
require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
processes = wmi.ExecQuery("select * from win32_process")
for process in processes do
cmdLine = process.CommandLine
puts "Command line: #{cmdLine}" if cmdLine =~ /mxmlc/
end
I've added in a regular expression to print out the command line only for processes which were started with "mxmlc" in the command line (which should work for your needs). For a more general solution of iterating over each process, just remove the if clause at the end of the line containing:
puts "Command line: #{cmdLine}" if cmdLine =~ /mxmlc/
This will save you the headache of doing any low-level magic with StartRemoteThread and navigating through the PEB structures.
That's about the best I could do considering the nature of your question and without more information regarding your development OS. If this solves your problem I might suggest that you edit your post so that people facing similar issues can find this solution. A title like "How to get command line arguments for a running process" might be more apt.

Resources