[solved] newbie error : signal NOT getting to slot!



  • Hi,
    I have a main window launching a child window, when the "start cpr" button is clicked on the child window, I want the main window to set up a TCP Listening thread. But I find, the signal does NOT get to the main window at all!

    The signal source is a push_button widget click. I change the widget name on click, and then call the main window to set up listener. The name change happens but the main window is not called.

    Can someone please spot the glaring error in what I am trying to do?

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtWidgets>
    #include "dynamicplots.h"
    #include "dispdata.h"
    #include "cprServer.h"

    namespace Ui {
    class MainWindow;
    }
    class dynamicPlots;
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget parent = 0);
    ~MainWindow();
    void setPlotWindow(dynamicPlots
    dp);

    private slots:
    void updatePeak(int val, int index);
    void on_quitButton_clicked();
    void slotAcceptUserLogin(QString&,QString&);
    void on_pbFiles_clicked();
    void setUpListener(bool);

    private:

    dynamicPlots *dPlot;
    Ui::MainWindow *ui;
     dispData* rawData;
     cprServer* server;
     int newPeak;
     int newRecoil;
     int newRate;
     int newTime;
    

    };

    #endif // MAINWINDOW_H
    #ifndef DYNAMICPLOTS_H
    #define DYNAMICPLOTS_H

    #include <QMainWindow>
    #include "qcustomplot.h"
    namespace Ui {
    class dynamicPlots;
    }

    class dynamicPlots : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit dynamicPlots(QWidget *parent = 0);
    ~dynamicPlots();
    void plotRealTime(QCustomPlot *plotW);
    void updateDepth(int);
    void updatePeakGraph(int val, double index);
    void setRate(int rate);
    void updateRecoilGraph(int val,double index);
    void updateRateGraph(int val,double index);

    signals:
    void startCPR(bool);

    public slots:
    void updatePeakGraph(int val, int index);
    void on_pbCPR_clicked();
    // void realtimeDataSlot();

    // void on_dial_valueChanged(int value);

    private slots:

    private:
    Ui::dynamicPlots *ui;
    QTimer dataTimer;
    };

    #endif // DYNAMICPLOTS_H
    #include "dynamicplots.h"
    #include "ui_dynamicplots.h"

    dynamicPlots::dynamicPlots(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::dynamicPlots)
    {
    ui->setupUi(this);
    plotRealTime(ui->plotW);
    ui->plotW->replot();

    }

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

    void dynamicPlots::updatePeakGraph(int val,int index)
    {
    double key = index/2000.0;

    static double lastPointKey = 0;
    ui->plotW->graph(0)->addData(key,val);
    ui->plotW->xAxis->setRange(key, 1.5, Qt::AlignRight);
    ui->plotW->graph(0)->rescaleValueAxis();
    ui->plotW->replot();
    updateDepth(val);
    

    }

    void dynamicPlots::setRate(int rate)
    {
    ui->rateVal->setObjectName(QString::number(rate));
    }

    void dynamicPlots::on_pbCPR_clicked()
    {
    if (ui->pbCPR->text() == "Start CPR")
    {
    ui->pbCPR->setText("Finished CPR");
    qDebug()<<"Emitting Started CPR";
    bool start = true;

        emit(startCPR(start));
    
    }
    

    }

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "cprserver.h"
    #include "logindialog.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    const QString IP = "127.0.0.1";
    const ushort port = 8100;
    ui->setupUi(this);
    rawData = new dispData (this);
    server = new cprServer(this,rawData,port);
    dPlot=new dynamicPlots(this);
    connect(rawData,SIGNAL(newPeakDepth(int,int)),this,SLOT(updatePeak(int, int)));
    connect(dPlot,SIGNAL(startCPR(bool)),this,SLOT(setUpListener(bool)));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow::updatePeak(int val,int index)
    {
    newPeak=val;
    newTime=index;
    double time = newTime/2000.0;
    this->dPlot->updateDepth(newPeak);
    this->dPlot->updatePeakGraph(newPeak,time);

    }

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

    void MainWindow::on_pbFiles_clicked()
    {
    LoginDialog* logDlg = new LoginDialog( this );
    connect( logDlg,
    SIGNAL(acceptLogin(QString&,QString&,int&)),
    this,
    SLOT(slotAcceptUserLogin(QString&,QString&)));

    //setUpListener();
    this->hide();
    logDlg->show();
    //dPlot->show();
    

    }

    void MainWindow::setUpListener(bool start)
    {
    const QString IP = "127.0.0.1";
    const ushort port = 8100;
    QHostAddress host;
    qDebug()<<"Inside Set Up for Listening "<<start;
    host.setAddress(IP);
    //server->setRawData(rawData);
    if (!server->listen(host,port)) {
    QMessageBox::critical(this, tr("CPR Server"),
    tr("Unable to start the server: %1.")
    .arg(server->errorString()));
    close();
    //qDebug() << "Unable to start server" << server.errorString();

    }
    QString ipAddress=(server->serverAddress()).toString();
    qDebug () << "Server started at " << ipAddress ;//go to log
    

    }

    void MainWindow::setPlotWindow(dynamicPlots* dp)
    {
    qDebug() << "Setting up Plot Window";
    dPlot = dp;
    }@


  • Moderators

    When you start the application in qt creator, it should tell you which connect is not established.
    Furthermore, by using connect's bool return value you can see which connect was not established. E.g.
    @
    #include <cassert>
    ...

    bool boo = connect ( ptr1, SIGNAL(somesignal), ptr2, SLOT(someslot)));
    assert ( boo );
    

    ...
    @



  • Assert does not fail, but still no action

    And thank you for including the <cassert>, I wondered how to add assert to qt before noticing your code in its entirety.


  • Moderators

    You mean lines 130-132 are executed but emit on line 134 fails?
    The connect in line 154 is carried out, but slot line 194 with qDebug in line 198 fails to be executed?
    Did you do a rebuild?
    Also in the debugger to can set a break point at the emit and another one in the slot.
    Anyway a rebuild is recommended.



  • I tried cleaning, setting breakpoints at the connect, emit and setUpListener lines:

    The debugger came out of the connect line with:
    returned value "2startCPR(bool)" char*

    Steeping into the emit signal functions goes to the MOC :
    @void dynamicPlots::startCPR(bool _t1)
    {
    void _a[] = { 0, const_cast<void>(reinterpret_cast<const void*>(&_t1)) };
    QMetaObject::activate(this, &staticMetaObject, 0, _a);
    }@

    Then, nothing happens. It just waits!



  • I am going a little nuts:
    @
    private:
    void setUpListener();
    Ui::MainWindow ui;
    dynamicPlots dPlot;
    dispData
    rawData;
    cprServer
    server;
    LoginDialog* logDlg;
    int newPeak;
    int newRecoil;
    int newRate;
    int newTime;
    };@

    signals from LoginDialog and dispData work fine!
    No action on signals from dynamicPlots!

    What could I be missing?!


  • Moderators

    You can use QObject's "dumpObjectInfo":http://qt-project.org/doc/qt-5/qobject.html#dumpObjectInfo and "dumpObjectTree":http://qt-project.org/doc/qt-5/qobject.html#dumpObjectTree for more information. It tells you about connections and stuff.

    It looks like your connection is no longer active when you arrive there. I wonder if it has something to do with QMainWindow, but I might confuse something.



  • I believe you are right . I cleaned everything up - mini test cases I had added along the way, multiple debug statements to chk status of my TCP or embedded usb data etc... and now it runs just fine. I had tried to set up things differently when I first started writing this code - every window is created in the main program and only has pointers to each other. It is possibly I had some legacy in some class from the earlier set up.

    Now, every class I need for the execution of my code is set up via the MainWindow class. Is this a good way to program though?

    Thanks so much for all your help!


  • Moderators

    Nice to read that your problem is solved.

    Please add [solved] to your title line of the initial post.


Log in to reply
 

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