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



  • 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?



  • @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



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


  • Qt Champions 2017

    @mranger90

    No, you have to omit clicked completely:

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


  • @Pablo-J.-Rogina

    That was it! Thanks a lot :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.