Sending information from one class to another.



  • Hi,

    I need a little assistance, I am trying to send a message from within a thread class to the mainWindow class. What I have done this far is.

    connect(threadClass, SIGNAL(sendMessage(const QString), mainWindowClass, SLOT(displayMessage(const QString)));
    

    sendMessage is a signal define in the thread class and display message is a slot defined in mainWindow class. When I compile it I get the following error

    expected primary-expression before ',' token
         connect(serverThread, SIGNAL(sendMessage(QString)), mainWindowClass, SLOT(updateLog(QString)));
                                                                             ^
    ``
    
    If I change mainWindowClass to "this", it will compile but when the signal is fired it obviously fails because the SLOT is not in "this" class.
    
    Any help is greately appreciated.
    
    Thanks
    
    Chris--


  • @Chrisw01
    Hi,friend,welcome devnet.

    can you try this

    connect(threadClass, &threadClass::sendMessage, mainWindowClass, &mainWindowClass::displayMessage);
    

    Maybe it will show other error or warning message.

    Can you tell me where is you wrote connect..., in cpp file?



  • Hi and thanks..

    I changed the code to:

    void speedySyncServer::incomingConnection(qintptr socketDescriptor)
    {
        qDebug() << "Connection Established!";
    
        speedySyncRemoteServerThread * serverThread = new speedySyncRemoteServerThread(socketDescriptor, this);
        connect(serverThread, SIGNAL(finished()), serverThread, SLOT(deleteLater()));
        connect(serverThread, &speedySyncRemoteServerThread::sendMessage, speedySyncRemote, &speedySyncRemote::updateLog);
        serverThread->start();
    }
    

    Same error...

    speedysyncserver.cpp:18: error: expected primary-expression before ',' token
         connect(serverThread, &speedySyncRemoteServerThread::sendMessage, speedySyncRemote, &speedySyncRemote::updateLog);
                                                                                           ^
    

    Basically, mainwindow class has a tcpServer. incomingConnection is overridden to start a QThread class. The signal is inside the QThread class the slot is inside the mainwindow class. the connection is in the TcpServer class. I think the issue is mainwindow is QWidget and the connect is looking for a QObject. If I change speedySyncRemote to "this" it will compile just output connection errors on the application output screen.



  • @Chrisw01

    connect(serverThread, &speedySyncRemoteServerThread::sendMessage, speedySyncRemote, &speedySyncRemote::updateLog);
    

    For this to work there has to be a pointer named speedySyncRemote of type speedySyncRemote available, either as a global variable or a member of speedySyncServer. Is that the case? (Probably is, because at least my gcc gives a different error message if the name is not recognized.)



  • @Chrisw01

    your code in speedySyncServer cpp file, why didn't you use this to replace the class name;

    void speedySyncServer::incomingConnection(qintptr socketDescriptor)
    {
        qDebug() << "Connection Established!";
    
        speedySyncRemoteServerThread * serverThread = new speedySyncRemoteServerThread(socketDescriptor, this);
        connect(serverThread, SIGNAL(finished()), serverThread, SLOT(deleteLater()));
        connect(serverThread, &speedySyncRemoteServerThread::sendMessage, this, &speedySyncRemote::updateLog); ///< can use this pointer?
        serverThread->start();
    }
    


  • @joeQ Why not SLOT is not in "this" class. ? where is your SLOT ?



  • I figured this out, because my mainWindow class has a QTcpServer in it...

    QTcpServer server;
    

    its parent object is null. So in my mainWindow I now call...

    server.setParent(this.parent());
    

    And then in my serverCkass I call...

    mainWindow *w = qobject_cast<mainWindow*>(parent());
        if(w!= 0) {
            speedySyncRemoteServerThread * serverThread = new speedySyncRemoteServerThread(socketDescriptor, this);
            connect(serverThread, SIGNAL(finished()), serverThread, SLOT(deleteLater()));
            connect(serverThread, &speedySyncRemoteServerThread::sendMessage, w, &speedySyncRemote::updateLog);
            serverThread->start();
        }
    

    All works like a champ now thread class is sending messages to mainwindows logFile.

    Thanks for all your help..


Log in to reply
 

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