Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Invalid use of void expression, when trying to connect signal/slot
Forum Updated to NodeBB v4.3 + New Features

Invalid use of void expression, when trying to connect signal/slot

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 5.6k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Christianvs
    wrote on last edited by
    #1

    Hi

    I got this QPushButton in my GUI which i want to connect to a public slot in an object called db of class database.

    The class database has following source:

    #include "database.h"
    
    database::database(QObject *parent) : QObject(parent)
    {
    
    }
    
    
    void database::storeData(rtl_msgs::current_set_point *rtlcsp, rtl_msgs::state *rtls)
    {
        //Storring rlt states and control params in a 2 dimensional vector
    
        QVector<float> rtlRowOfData;
    
        rtlRowOfData.push_back(rtls->position);
        rtlRowOfData.push_back(rtls->velocity);
        rtlRowOfData.push_back(rtls->force);
        rtlRowOfData.push_back(rtlcsp->target_position);
        rtlRowOfData.push_back(rtlcsp->target_velocity);
        rtlRowOfData.push_back(rtlcsp->target_force);
    
    
        rtlData.push_back(rtlRowOfData);
    }
    
    
    void database::saveData()
    {
        std::ofstream rtlDataStream;
        rtlDataStream.open("rtlData.csv");
    
        if(rtlDataStream.is_open())
        {
            rtlDataStream << "Position,Velocity,Force,Target position,Target velocity, Target force" << std::endl;
    
            // For each loop
            for(const auto& singleRow : rtlData){
            for(const auto& singleVal : singleRow){
            rtlDataStream << singleVal << ",";
            }
            rtlDataStream << std::endl;
            }
    
            ROS_INFO_STREAM("Data saved");
        }
        else
        {
            ROS_INFO_STREAM("Stream not open");
        }
    
        rtlDataStream.close();
    }
    
    

    And the following header:

    #ifndef DATABASE_H
    #define DATABASE_H
    
    #include <QObject>
    #include <QVector>
    #include <rtl_msgs/state.h>
    #include <rtl_msgs/current_set_point.h>
    #include "ros_comm.h"
    #include "mainwindow.h"
    #include <iostream>
    #include <fstream>
    
    class database : public QObject
    {
        Q_OBJECT
    public:
        explicit database(QObject *parent = 0);
    
        void storeData(rtl_msgs::current_set_point *rtlcsp, rtl_msgs::state *rtls);
    
    
    private:
        QVector< QVector<float> > rtlData;
    
    signals:
    
    public slots:
        void saveData();
    };
    
    #endif // DATABASE_H
    
    

    Now when i initialize my MainWindow i create an object db of class database and connect the QPushButton->clicked with the slot database::saveData as follows:

    MainWindow::MainWindow(int argc, char **argv, QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        ros_comm_(argc, argv)
    {
        ui->setupUi(this);
    
        database *db = new database(this);
    //    connect(ui, SIGNAL(clicked()), db, SLOT(saveData()));
        connect(ui->saveRTLData->clicked(), &QPushButton::clicked, db, &database::saveData);
        
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(gui_update()));
        timer->start(10);
    }
    

    I tried both the new and old connect and cannot seem to get any of them working.

    I get following error in my console when i try to compile:

    0_1520876249816_c5d55504-ff12-4ee3-ba31-48df85935550-image.png

    Can anyone see what i did wrong?

    1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @Christianvs said in Invalid use of void expression, when trying to connect signal/slot:

      connect(ui->saveRTLData->clicked(), &QPushButton::clicked, db, &database::saveData);
      

      I guess you are providing the signal but not the sender object, ui->saveRTLData->clicked() instead of ui->saveRTLData as required

      connect(
          sender, &Sender::signal,
          receiver, &Receiver::slot
      );
      

      There's more information about new syntax of signals & slots

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      C 1 Reply Last reply
      4
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on last edited by
        #3

        I think the problem is the parentheses on ui->saveRTLData->clicked().

        aha_1980A 1 Reply Last reply
        1
        • mranger90M mranger90

          I think the problem is the parentheses on ui->saveRTLData->clicked().

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mranger90

          No, you have to omit clicked completely:

          connect(ui->saveRTLData, &QPushButton::clicked, db, &database::saveData);
          

          Qt has to stay free or it will die.

          1 Reply Last reply
          3
          • Pablo J. RoginaP Pablo J. Rogina

            @Christianvs said in Invalid use of void expression, when trying to connect signal/slot:

            connect(ui->saveRTLData->clicked(), &QPushButton::clicked, db, &database::saveData);
            

            I guess you are providing the signal but not the sender object, ui->saveRTLData->clicked() instead of ui->saveRTLData as required

            connect(
                sender, &Sender::signal,
                receiver, &Receiver::slot
            );
            

            There's more information about new syntax of signals & slots

            C Offline
            C Offline
            Christianvs
            wrote on last edited by
            #5

            @Pablo-J.-Rogina

            That was it! Thanks a lot :)

            1 Reply Last reply
            1

            • Login

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