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. Why in class QTcpServer slot incomingConnection that is override virtual not works?

Why in class QTcpServer slot incomingConnection that is override virtual not works?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 628 Views 1 Watching
  • 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    Why in class QTcpServer slot incomingConnection that overrides virtual not works?

    I used this tutorial( Multithread Client/Server) to create qtcpsocket. But when I send a message from the client to the server the connection is not starting... I expect when a connection is received in a different thread qtcpsocket is connected and prints the message of the client.
    Thanks in advance

    // myserver.h
    
    #ifndef MYSERVER_H
    #define MYSERVER_H
    
    #include <QTcpServer>
    #include <QDebug>
    #include "mythread.h"
    
    class MyServer : public QTcpServer
    {
        Q_OBJECT
    public:
        explicit MyServer(QObject *parent = 0);
        void StartServer();
        
    signals:
        
    public slots:
    
    protected:
        void incomingConnection(int socketDescriptor);
        
    };
    
    #endif // MYSERVER_H
    
    // myserver.cpp
    
    #include "myserver.h"
    
    MyServer::MyServer(QObject *parent) :
        QTcpServer(parent)
    {
    }
    
    void MyServer::StartServer()
    {
        if(!this->listen(QHostAddress::Any,1234))
        {
            qDebug() << "Could not start server";
        }
        else
        {
            qDebug() << "Listening...";
        }
    }
    
    void MyServer::incomingConnection(int socketDescriptor)
    {
        qDebug() << socketDescriptor << " Connecting...";
        MyThread *thread = new MyThread(socketDescriptor, this);
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • stackprogramerS Offline
      stackprogramerS Offline
      stackprogramer
      wrote on last edited by
      #5

      Finally, I change the definition and declaration of incomingConnection slot according to below now everything is ok...

      #ifndef MYSERVER_H
      #define MYSERVER_H
      
      #include <QTcpServer>
      #include <QDebug>
      #include "mythread.h"
      
      class MyServer : public QTcpServer
      {
          Q_OBJECT
      public:
          explicit MyServer(QObject *parent = 0);
          void StartServer();
          
      signals:
          
      public slots:
      
      protected:
          void incomingConnection(qint64 socketDescriptor);
          
      };
      
      #endif // MYSERVER_H
      
      
      #include "myserver.h"
      
      MyServer::MyServer(QObject *parent) :
          QTcpServer(parent)
      {
      }
      
      void MyServer::StartServer()
      {
          if(!this->listen(QHostAddress::Any,1234))
          {
              qDebug() << "Could not start server";
          }
          else
          {
              qDebug() << "Listening...";
          }
      }
      
      void MyServer::incomingConnection(qint64 socketDescriptor)
      {
          qDebug() << socketDescriptor << " Connecting...";
          MyThread *thread = new MyThread(socketDescriptor, this);
          connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
          thread->start();
      }
      
      
      1 Reply Last reply
      0
      • stackprogramerS stackprogramer

        Why in class QTcpServer slot incomingConnection that overrides virtual not works?

        I used this tutorial( Multithread Client/Server) to create qtcpsocket. But when I send a message from the client to the server the connection is not starting... I expect when a connection is received in a different thread qtcpsocket is connected and prints the message of the client.
        Thanks in advance

        // myserver.h
        
        #ifndef MYSERVER_H
        #define MYSERVER_H
        
        #include <QTcpServer>
        #include <QDebug>
        #include "mythread.h"
        
        class MyServer : public QTcpServer
        {
            Q_OBJECT
        public:
            explicit MyServer(QObject *parent = 0);
            void StartServer();
            
        signals:
            
        public slots:
        
        protected:
            void incomingConnection(int socketDescriptor);
            
        };
        
        #endif // MYSERVER_H
        
        // myserver.cpp
        
        #include "myserver.h"
        
        MyServer::MyServer(QObject *parent) :
            QTcpServer(parent)
        {
        }
        
        void MyServer::StartServer()
        {
            if(!this->listen(QHostAddress::Any,1234))
            {
                qDebug() << "Could not start server";
            }
            else
            {
                qDebug() << "Listening...";
            }
        }
        
        void MyServer::incomingConnection(int socketDescriptor)
        {
            qDebug() << socketDescriptor << " Connecting...";
            MyThread *thread = new MyThread(socketDescriptor, this);
            connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
            thread->start();
        }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @stackprogramer please, when you override something, use the override keyword! the compiler would have told you, that you're trying the override a function, that does not exist/is not virtual.

        The function declaration is:

        void QTcpServer::incomingConnection(qintptr)

        which is clearly different from your

        MyServer::incomingConnection(int)


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • stackprogramerS Offline
          stackprogramerS Offline
          stackprogramer
          wrote on last edited by
          #3

          I have downloaded this example from the site I mentioned in the link. With these interpretations, what changes should I make in the code in order for data to arrive and a new thread to be executed?

          I saw this tutorial, override base function is the same as it is done in up example...
          https://www.programiz.com/cpp-programming/function-overriding

          J.HilkJ 1 Reply Last reply
          0
          • stackprogramerS stackprogramer

            I have downloaded this example from the site I mentioned in the link. With these interpretations, what changes should I make in the code in order for data to arrive and a new thread to be executed?

            I saw this tutorial, override base function is the same as it is done in up example...
            https://www.programiz.com/cpp-programming/function-overriding

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #4

            @stackprogramer

            void incomingConnection(qintptr socketDescriptor) override;

            the original creator did it "wrong" as well, no override mark, and int instead of qintptr, which was probably fine for him at the time.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • stackprogramerS Offline
              stackprogramerS Offline
              stackprogramer
              wrote on last edited by
              #5

              Finally, I change the definition and declaration of incomingConnection slot according to below now everything is ok...

              #ifndef MYSERVER_H
              #define MYSERVER_H
              
              #include <QTcpServer>
              #include <QDebug>
              #include "mythread.h"
              
              class MyServer : public QTcpServer
              {
                  Q_OBJECT
              public:
                  explicit MyServer(QObject *parent = 0);
                  void StartServer();
                  
              signals:
                  
              public slots:
              
              protected:
                  void incomingConnection(qint64 socketDescriptor);
                  
              };
              
              #endif // MYSERVER_H
              
              
              #include "myserver.h"
              
              MyServer::MyServer(QObject *parent) :
                  QTcpServer(parent)
              {
              }
              
              void MyServer::StartServer()
              {
                  if(!this->listen(QHostAddress::Any,1234))
                  {
                      qDebug() << "Could not start server";
                  }
                  else
                  {
                      qDebug() << "Listening...";
                  }
              }
              
              void MyServer::incomingConnection(qint64 socketDescriptor)
              {
                  qDebug() << socketDescriptor << " Connecting...";
                  MyThread *thread = new MyThread(socketDescriptor, this);
                  connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
                  thread->start();
              }
              
              
              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