Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt GUI + ROS: where to put the ros parameters?

Qt GUI + ROS: where to put the ros parameters?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joanagilf
    wrote on last edited by
    #1

    Hi all!

    So, I created a Qt UI in a ROS node and after some fights with CMakeLists I managed to compile it with catkin.

    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())...)

    This is my mainwindow.cpp code (except some callbacks related to some buttons):

    #include "ros/ros.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtGui/QPushButton>
    #include "globals.h"
    #include <QString>
    #include <string>
    #include <sstream>
    #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->setupUi(this);
        ui->spinBox1->hide();
        ui->spinBox2->hide();
        ui->spinBox3->hide();
        ui->spinBox4->hide();
        ui->spinBox5->hide();
        ui->spinBox6->hide();
        ui->spinBox7->hide();
        ui->spinBox8->hide();
        ui->spinBox9->hide();
        ui->spinBox10->hide();
        ui->spinBox11->hide();
        ui->spinBox12->hide();
        ui->spinBox13->hide();
        ui->spinBox14->hide();
        ui->spinBox15->hide();
        ui->spinBox16->hide();
        ui->spinBoxall->hide();
        numberOfMotors =0;
    
        int argc = 0;
        char **argv;
        ros::init(argc, argv, "qtgui");
        cout << "init" << endl;
    
     /*   TIMER */
       QTimer *timer = new  QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
        timer->start(1000);
        showTime();
    }
    
    void MainWindow::showTime()
    {
        QTime time = QTime::currentTime();
        QString text = time.toString("hh:mm");
        if ((time.second() % 2) == 0)
            text[2] = ' ';
        cout << "text" << 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 << "nodehandle" << endl;
        ros::Publisher QtPublisher = n.advertise<qtgui::GUIDados>("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<16;i++)
        {
            msg.intensidade[i] = intensidade[i];
            msg.local[i] = local[i];
        }
        QtPublisher.publish(msg);
        ros::spinOnce();
    
        loop_rate.sleep();
       }
    }
    

    What I have so far is that, when I click on send it sends a ros message. I tried to initialize ros in the *MainWindow::MainWindow(QWidget parent) however it didnt exactly work...

    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.
    Where should I put my ros parameters so that this works fine?

    Here's my other parts of the code:
    main.cpp:

    #include "mainwindow.h"
    #include <QtGui/QApplication>
    #include "globals.h"
    #include "qtgui/GUIDados.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        std::cout<<"I am Alive!"<<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
    }
    
    

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    
    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
    

    Thank you in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        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 ?

        J Offline
        J Offline
        joanagilf
        wrote on last edited by
        #3

        @SGaist Hi and thank you!

        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..).

        I'm gonna try the way you said it, and I'll write here what I managed to do.

        Thanks!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Before going further: did you saw the Qt ROS page ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          1
          • SGaistS SGaist

            Before going further: did you saw the Qt ROS page ?

            J Offline
            J Offline
            joanagilf
            wrote on last edited by
            #5

            @SGaist Yes!

            1 Reply Last reply
            0
            • J Offline
              J Offline
              joanagilf
              wrote on last edited by
              #6

              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!

              Thanks for the help! :)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Great !

                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 :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved