简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM