<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Qt GUI + ROS: where to put the ros parameters?]]></title><description><![CDATA[<p dir="auto">Hi all!</p>
<p dir="auto">So, I created a Qt UI in a ROS node and after some fights with CMakeLists I managed to compile it with catkin.</p>
<p dir="auto">Now the problem that I have is that I don't know exactly where to put my ROS parameters in the mainwindow.cpp. I have it in a way where I can send messages when I click on the button "Send". However, it only works once because after that the app crashes (I assume the system gets trapped in the while(ros::ok())...)</p>
<p dir="auto">This is my <strong>mainwindow.cpp</strong> code (except some callbacks related to some buttons):</p>
<pre><code>#include "ros/ros.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include &lt;QtGui/QPushButton&gt;
#include "globals.h"
#include &lt;QString&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include "QMessageBox"
#include "QTimer"
#include "QTime"
#include "qtgui/GUIDados.h"

using namespace std;
using namespace ros;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui-&gt;setupUi(this);
    ui-&gt;spinBox1-&gt;hide();
    ui-&gt;spinBox2-&gt;hide();
    ui-&gt;spinBox3-&gt;hide();
    ui-&gt;spinBox4-&gt;hide();
    ui-&gt;spinBox5-&gt;hide();
    ui-&gt;spinBox6-&gt;hide();
    ui-&gt;spinBox7-&gt;hide();
    ui-&gt;spinBox8-&gt;hide();
    ui-&gt;spinBox9-&gt;hide();
    ui-&gt;spinBox10-&gt;hide();
    ui-&gt;spinBox11-&gt;hide();
    ui-&gt;spinBox12-&gt;hide();
    ui-&gt;spinBox13-&gt;hide();
    ui-&gt;spinBox14-&gt;hide();
    ui-&gt;spinBox15-&gt;hide();
    ui-&gt;spinBox16-&gt;hide();
    ui-&gt;spinBoxall-&gt;hide();
    numberOfMotors =0;

    int argc = 0;
    char **argv;
    ros::init(argc, argv, "qtgui");
    cout &lt;&lt; "init" &lt;&lt; endl;

 /*   TIMER */
   QTimer *timer = new  QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    timer-&gt;start(1000);
    showTime();
}

void MainWindow::showTime()
{
    QTime time = QTime::currentTime();
    QString text = time.toString("hh:mm");
    if ((time.second() % 2) == 0)
        text[2] = ' ';
    cout &lt;&lt; "text" &lt;&lt; endl;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_closeButton_clicked()
{
    close();
}

void MainWindow::on_sendButton_clicked()
{
    QMessageBox::information(this,tr("Titulo"),tr("escolheu: %1").arg( intensidade[0])); //dutycycle do motor1
      ros::NodeHandle n;
    cout &lt;&lt; "nodehandle" &lt;&lt; endl;
    ros::Publisher QtPublisher = n.advertise&lt;qtgui::GUIDados&gt;("guichattergui",1000);
    ros::Rate loop_rate(10); //10hz
    while(ros::ok())
   {
    qtgui::GUIDados msg;

    msg.numeroMotores = numberOfMotors;
    msg.intensidade.resize(16);
    msg.local.resize(16);
    for(int i=0;i&lt;16;i++)
    {
        msg.intensidade[i] = intensidade[i];
        msg.local[i] = local[i];
    }
    QtPublisher.publish(msg);
    ros::spinOnce();

    loop_rate.sleep();
   }
}
</code></pre>
<p dir="auto">What I have so far is that, when I click on send it sends a ros message. I tried to initialize ros in the *<em>MainWindow::MainWindow(QWidget <em>parent)</em></em> however it didnt exactly work...</p>
<p dir="auto">As you probably can guess, I'm very new to this Qt interface thing (and also ROS to be fair), so any help would be useful.<br />
Where should I put my ros parameters so that this works fine?</p>
<p dir="auto">Here's my other parts of the code:<br />
<strong>main.cpp:</strong></p>
<pre><code>#include "mainwindow.h"
#include &lt;QtGui/QApplication&gt;
#include "globals.h"
#include "qtgui/GUIDados.h"

using namespace std;

int main(int argc, char *argv[])
{
    std::cout&lt;&lt;"I am Alive!"&lt;&lt;std::endl;

    QApplication a(argc, argv); //creates a QApplication object

    MainWindow w; //creates the main window object
    w.show();

    return a.exec(); //enter its event loop
}

</code></pre>
<p dir="auto"><strong>mainwindow.h:</strong></p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include &lt;QMainWindow&gt;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT //declares our class as a QObject

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void showTime();

    void on_closeButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
</code></pre>
<p dir="auto">Thank you in advance!</p>
]]></description><link>https://forum.qt.io/topic/78899/qt-gui-ros-where-to-put-the-ros-parameters</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Jul 2026 12:25:16 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/78899.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 03 May 2017 14:07:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt GUI + ROS: where to put the ros parameters? on Mon, 08 May 2017 19:59:22 GMT]]></title><description><![CDATA[<p dir="auto">Great !</p>
<p dir="auto">Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)</p>
]]></description><link>https://forum.qt.io/post/392430</link><guid isPermaLink="true">https://forum.qt.io/post/392430</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 08 May 2017 19:59:22 GMT</pubDate></item><item><title><![CDATA[Reply to Qt GUI + ROS: where to put the ros parameters? on Mon, 08 May 2017 14:16:03 GMT]]></title><description><![CDATA[<p dir="auto">Done! I created a class with my QtPublisher where I initialize my publisher and where I created a function that sends the messages! Now it works perfectly!</p>
<p dir="auto">Thanks for the help! :)</p>
]]></description><link>https://forum.qt.io/post/392380</link><guid isPermaLink="true">https://forum.qt.io/post/392380</guid><dc:creator><![CDATA[joanagilf]]></dc:creator><pubDate>Mon, 08 May 2017 14:16:03 GMT</pubDate></item><item><title><![CDATA[Reply to Qt GUI + ROS: where to put the ros parameters? on Mon, 08 May 2017 09:07:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Yes!</p>
]]></description><link>https://forum.qt.io/post/392310</link><guid isPermaLink="true">https://forum.qt.io/post/392310</guid><dc:creator><![CDATA[joanagilf]]></dc:creator><pubDate>Mon, 08 May 2017 09:07:31 GMT</pubDate></item><item><title><![CDATA[Reply to Qt GUI + ROS: where to put the ros parameters? on Thu, 04 May 2017 21:08:44 GMT]]></title><description><![CDATA[<p dir="auto">Before going further: did you saw the <a href="http://wiki.ros.org/qt_ros" target="_blank" rel="noopener noreferrer nofollow ugc">Qt ROS</a> page ?</p>
]]></description><link>https://forum.qt.io/post/391813</link><guid isPermaLink="true">https://forum.qt.io/post/391813</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 04 May 2017 21:08:44 GMT</pubDate></item><item><title><![CDATA[Reply to Qt GUI + ROS: where to put the ros parameters? on Thu, 04 May 2017 08:40:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Hi and thank you!</p>
<p dir="auto">I have no idea.. As I said, I'm very new to this all ROS and Qt and even c++ thing (the only programming I ever had was matlab for 1 year and C for 1 semester..).</p>
<p dir="auto">I'm gonna try the way you said it, and I'll write here what I managed to do.</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.qt.io/post/391633</link><guid isPermaLink="true">https://forum.qt.io/post/391633</guid><dc:creator><![CDATA[joanagilf]]></dc:creator><pubDate>Thu, 04 May 2017 08:40:29 GMT</pubDate></item><item><title><![CDATA[Reply to Qt GUI + ROS: where to put the ros parameters? on Wed, 03 May 2017 22:09:10 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">From the looks of it, shouldn't you rather have a dedicated object handling the ROS part and it's infinite loop running in a different thread and then have your GUI talk with it ?</p>
]]></description><link>https://forum.qt.io/post/391551</link><guid isPermaLink="true">https://forum.qt.io/post/391551</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 03 May 2017 22:09:10 GMT</pubDate></item></channel></rss>