How to use signals and slots if signal is emited from pointed object?



  • How to use signals and slots if signal is emited from seperate object that has pointer?

    for example this example will not work:

    connect( testpointer , testpointer->got_new_data() , this , send() )
    testpointer is pointer to seperate tcpsocket class.
    signal got_new_data() is declared under tcpsocket class and also emitted under same class.
    connect( testpointer , testpointer->got_new_data() , this , send() ) is under seperate class.



  • @Q139 I'd like to help you but please explain your code better. I'm afraid i don't quite understand your application's structure. Would you be so kind as to post your class and signals declarations and other relevant information?



  • Doesn't this work:

    connect(testpointer, SIGNAL(got_new_data()), this, SLOT(send()));
    


  • example:

    class MyObject : public QObject {
        Q_OBJECT
    signals:
        void someSignal();
    
    public slots:
        void someSlot();
    };
     ...
        //old syntax
        QObject::connect(p1, SIGNAL(someSignal()), p2, SLOT(someSlot()));
        
        //new syntax
        QObject::connect(p1, &MyObject::someSignal, p2, &MyObject::someSlot);  
    

    bottom line,
    replace

    connect( testpointer , testpointer->got_new_data() , this , send() )
    

    with something line

    connect( testpointer ,SIGNAL(got_new_data()) , this , SLOT(send()))
    


  • Thanks for all examples and help but for some reason app returns QObject::connect: Cannot connect (null)::got_last_data_signal() to test::randomize_and_send_data()

    #ifndef TCP_SERVER_FILE_H
    #define TCP_SERVER_FILE_H
    
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    
    
    class TCP_Socket : public QObject
    {
        Q_OBJECT
    public:
        QString symbol_name = "-not recived";
        void New_socket(QTcpSocket *ptr);
        void send_data(QByteArray bytes);
        QTcpSocket *ConSocket;
    
    
       void got_last_data(double t, double h, double b);
    
    double LAST_t;
    double LAST_h;
    double LAST_b;
    
    signals:
       void got_last_data_signal();
    private:
    
    
    public slots:
         //ConSocket
         void readyReadCon();
         void connected();
         void disconnected();
         void bytesWritten(qint64 bytes);
    
    
    };
    
    class TCP_Server_File : public QObject
    {
        Q_OBJECT
    public:
        explicit TCP_Server_File(QObject *parent = 0);
    
         int Client_count;
         void set_adress_and_port_to_listen(int port);
    
         QTcpServer *server;
    public slots:
    
        //server
        void NewConnection();
    
    };
    extern TCP_Server_File TCP_IN_Server;
    
    #endif // TCP_SERVER_FILE_H
    
    
    tcp.cpp
    
    
    void TCP_Socket::got_last_data(double t, double h, double b)
    {
        LAST_t = t;
        LAST_h = h;
        LAST_b = b;
        emit got_last_data_signal();
    }
    
    
    ----------------------------------------------------------------------------------------
    TEST.H
    
    #ifndef TEST_H
    #define TEST_H
    
    #include <QWidget>
    #include <qobject.h>
    #include "QTimer"
    #include "tcp_server_file.h"
    
    
    namespace Ui {
    class test;
    }
    
    class test : public QWidget
    {
        Q_OBJECT
    
    public:
    
        TCP_Socket*test_pointer;
        int cc=0;
    
    
        explicit test(QWidget *parent = 0);
        ~test();
    
    void gui_refresh();
    
    
        double timing_to_add_ms = 0;
    
            void update_set();
    public slots:
        void timer_out();
    
        void randomize_and_send_data();
    
        void on_hour_valueChanged(int arg1);
    
        void on_min_valueChanged(int arg1);
    
        void on_sec_valueChanged(int arg1);
    
        void on_msec_valueChanged(int arg1);
    
    
        void on_auto_on_off_clicked();
    
        void on_pushButton_4_clicked();
    
    
        void on_pushButton_2_clicked();
    
        void on_pushButton_clicked();
    
        void on_pushButton_3_clicked();
    
    private slots:
        void on_pushButton_5_clicked();
    
        void on_pushButton_6_clicked();
    
    private:
        Ui::test *ui;
    
    };
    
    #endif // TEST_H
    ---------------------------------------------------------------------------------------------------
    TEST.CPP
    
    -
    void test::on_auto_on_off_clicked()
    {
        if(ui->auto_on_off->isChecked()){
        tim->start(ui->intervalMS->value());
        connect(tim, SIGNAL(timeout()), this, SLOT(timer_out()));
        connect(test_pointer, SIGNAL( got_last_data_signal() ) , this , SLOT( randomize_and_send_data() ) );  // not connecting
        }else{
        tim->stop();
        //discon
        }
    }
    
    ---------------------------------------------------------------------------
    

    Starting O:\proj\build-S-Desktop_Qt_5_5_1_MinGW_32bit-Debug\debug\S.exe...
    QObject::connect: Cannot connect (null)::got_last_data_signal() to test::randomize_and_send_data()

    [edit: added missing coding tags SGaist]


  • Lifetime Qt Champion

    Hi,

    Looks like you never initialize test_pointer so luckily you have a null pointer.

    Please surround your code with ``` three back ticks, that will make it more readable.



  • Yes found this out some minutes ago.
    Thanks for all examples and help .
    Sorry to waste ur time problem was due to no client connected and no valid adress to pointer assigned.


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now please mark the thread as solved using the "Topic Tools" button so other forum users may know a solution has been found :)

    Also, while browsing the forum, consider up-voting answers that helped you, that will make them easier to find for other people :)


Log in to reply
 

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