Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending information from one class to another.

Sending information from one class to another.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chrisw01
    wrote on last edited by
    #1

    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--
    joeQJ 1 Reply Last reply
    0
    • C Chrisw01

      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--
      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @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?

      Just do it!

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chrisw01
        wrote on last edited by
        #3

        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.

        E joeQJ 2 Replies Last reply
        0
        • C Chrisw01

          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.

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @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.)

          1 Reply Last reply
          0
          • C Chrisw01

            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.

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

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

            Just do it!

            joeQJ 1 Reply Last reply
            0
            • joeQJ joeQ

              @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();
              }
              
              joeQJ Offline
              joeQJ Offline
              joeQ
              wrote on last edited by
              #6

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

              Just do it!

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chrisw01
                wrote on last edited by
                #7

                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..

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved