Giuseppe VERGARI
Giuseppe VERGARI

Reputation: 39

Qt Signal/Slot Issue

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.

Upvotes: 1

Views: 72

Answers (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 ;
}

Upvotes: 1

Related Questions