1

I can't figure out what's going wrong here. I want to pass a value from a class to another. Here's the code:

mainwindow.h

public slots:
    void printNumbers(int);

mainwindow.cpp

void MainWindow::printNumbers(int a)
{
    qDebug() << a;
}

myudp.h

signals:
    inline void sendBuff(int);

myudp.cpp

        [***]
        MainWindow *widget = new MainWindow();
        connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
        const bool connected = connect(this , SIGNAL(sendBuff(int)), widget ,SLOT(printNumbers(int)));
        qDebug() << "Connection established?" << connected;
        [***]

    void MyUDP::readyRead()
    {
        // when data comes in
        emit sendBuff(13);

        [***]
    }

    inline void MyUDP::sendBuff(int a)
    {
        qDebug() << "sending " << a ;
    }

Main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMainWindow window;
    MainWindow *widget = new MainWindow();
    window.setCentralWidget(widget);
    window.resize(900, 600);
    window.show();
    MyUDP *client = new MyUDP();
    return a.exec();
}

I used "inline" because of an error: duplicate MyUDP::sendBuff(int a).

I don't know if it can be an issue.

when I execute the "emit sendBuff(12)" I only receive "sending 12", I didn't catch the printNumbers()'s output even if the variable "connected" is true.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Giuseppe VERGARI
  • 39
  • 1
  • 1
  • 4
  • Did you check signal is emitted ? you need to debug `MyUDP::readyRead()` and make sure you socket is working and the `readyread()` is fired – Mohammad Kanan Jun 12 '20 at 01:28

1 Answers1

1

remove the inline declaration of the slot, and if your class needs an internal use for sendBuff then declare a new method for that...

signals:
    void sendBuffSignal(int);

and in the cpp

void MyUDP::sendBuff(int a)
{
    qDebug() << "sending " << a ;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Thanks, mate. I figure out to remove the entire sendBuff declaration and it works. But If I remove only the inline declaration it returns a duplicate declaration error. Just to know, do you have any ideas for this issue? – Giuseppe VERGARI Jun 12 '20 at 10:47