簡體   English   中英

Qt 信號/插槽問題

[英]Qt Signal/Slot Issue

我無法弄清楚這里出了什么問題。 我想將一個值從 class 傳遞給另一個。 這是代碼:

主窗口.h

public slots:
    void printNumbers(int);

主窗口.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 ;
    }

主文件

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

由於錯誤,我使用了“內聯”:重復 MyUDP::sendBuff(int a)。

我不知道這是否可能是一個問題。

當我執行“ emit sendBuff(12) ”時,我只收到“ sending 12 ”,即使變量“ connected ”為真,我也沒有捕捉到printNumbers()的 output。

刪除插槽的內聯聲明,如果您的 class 需要內部使用sendBuff然后聲明一個新方法...

signals:
    void sendBuffSignal(int);

並在 cpp

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM