Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Multi threaded TCP server GUI using Qt widgets application
Forum Updated to NodeBB v4.3 + New Features

Multi threaded TCP server GUI using Qt widgets application

Scheduled Pinned Locked Moved Unsolved Qt 6
16 Posts 3 Posters 1.8k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Qt has a threaded server example: https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    F 1 Reply Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      Qt has a threaded server example: https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html

      F Offline
      F Offline
      fari35
      wrote on last edited by
      #3

      @Christian-Ehrlicher I know how to make a threaded tcp server console application but I am having issues in making a gui of it. I have edited my question you can see the error there.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        You should also define your function MyServer::startServer() I would guess.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        F 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          You should also define your function MyServer::startServer() I would guess.

          F Offline
          F Offline
          fari35
          wrote on last edited by
          #5

          @Christian-Ehrlicher yes its defined in server.h file :

          void MyServer::startServer()
          {
              int port = 54000;
          
              if(!this->listen(QHostAddress::Any,port))
              {
                  qDebug() << "Could not start server";
              }
              else
              {
                  qDebug() << "Listening to port " << port << "...";
              }
          }
          

          I am using the same code as in this link:"https://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php"

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            And you added the server.cpp to your pro-file?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            F 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              And you added the server.cpp to your pro-file?

              F Offline
              F Offline
              fari35
              wrote on last edited by
              #7

              @Christian-Ehrlicher yes, see I have added all the files:

              QT       += core
              QT       +=network
              QT       +=gui
              greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
              
              CONFIG += c++11
              
              # You can make your code fail to compile if it uses deprecated APIs.
              # In order to do so, uncomment the following line.
              #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
              TEMPLATE = app
              SOURCES += \
                  main.cpp \
                  mainwindow.cpp
                  myserver.cpp
                  mythread.cpp
              
              HEADERS += \
                  mainwindow.h
                  myserver.h
                  mythread.h
              
              FORMS += \
                  mainwindow.ui
              
              # Default rules for deployment.
              qnx: target.path = /tmp/$${TARGET}/bin
              else: unix:!android: target.path = /opt/$${TARGET}/bin
              !isEmpty(target.path): INSTALLS += target
              
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @fari35 said in Multi threaded TCP server GUI using Qt widgets application:

                > SOURCES += \
                >     main.cpp \
                >     mainwindow.cpp
                >     myserver.cpp
                >     mythread.cpp
                

                This does not look correct - you forgot the \

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                F 1 Reply Last reply
                4
                • Christian EhrlicherC Christian Ehrlicher

                  @fari35 said in Multi threaded TCP server GUI using Qt widgets application:

                  > SOURCES += \
                  >     main.cpp \
                  >     mainwindow.cpp
                  >     myserver.cpp
                  >     mythread.cpp
                  

                  This does not look correct - you forgot the \

                  F Offline
                  F Offline
                  fari35
                  wrote on last edited by fari35
                  #9

                  @Christian-Ehrlicher Do I need to put this \ in front of every file?

                  JonBJ 1 Reply Last reply
                  0
                  • F fari35

                    @Christian-Ehrlicher Do I need to put this \ in front of every file?

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #10

                    @fari35
                    No, it's a line continuation character. It must go at the end of each line in a list of file names other than the final one.

                    F 1 Reply Last reply
                    4
                    • JonBJ JonB

                      @fari35
                      No, it's a line continuation character. It must go at the end of each line in a list of file names other than the final one.

                      F Offline
                      F Offline
                      fari35
                      wrote on last edited by
                      #11

                      @JonB I have checked that but still I'm getting the same error. I have added all the files with their code in my question. Can you please tell me what I'm doing wrong here?

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Please post your pro file and start over with a clean build dir

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        F 1 Reply Last reply
                        2
                        • Christian EhrlicherC Christian Ehrlicher

                          Please post your pro file and start over with a clean build dir

                          F Offline
                          F Offline
                          fari35
                          wrote on last edited by
                          #13

                          @Christian-Ehrlicher This is my pro file:

                          QT       += core
                          QT       +=network
                          QT       +=gui
                          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                          
                          CONFIG += c++11
                          
                          # You can make your code fail to compile if it uses deprecated APIs.
                          # In order to do so, uncomment the following line.
                          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                          TEMPLATE = app
                          SOURCES += \
                              main.cpp \
                              mainwindow.cpp\
                              myserver.cpp
                              mythread.cpp
                          
                          HEADERS += \
                              mainwindow.h\
                              myserver.h
                              mythread.h
                          
                          FORMS += \
                              mainwindow.ui
                          
                          # Default rules for deployment.
                          qnx: target.path = /tmp/$${TARGET}/bin
                          else: unix:!android: target.path = /opt/$${TARGET}/bin
                          !isEmpty(target.path): INSTALLS += target
                          
                          
                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            You did notice what @JonB and I told you? You forgot to add the \ ...

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            F 1 Reply Last reply
                            1
                            • Christian EhrlicherC Christian Ehrlicher

                              You did notice what @JonB and I told you? You forgot to add the \ ...

                              F Offline
                              F Offline
                              fari35
                              wrote on last edited by fari35
                              #15

                              @Christian-Ehrlicher @JonB yes Now its working but when I'm trying to create an object of MainWindow class in server.h class so I'm not able to create that. Cany you please tell me how can I make the object of MainWindow class?
                              server.h:

                              #ifndef SERVER_H
                              #define SERVER_H
                              #include <QStringList>
                              #include <QTcpServer>
                              #include "mainwindow.h"
                              class server : public QTcpServer
                              {
                                  Q_OBJECT
                              
                              public:
                                  server(QObject *parent = nullptr);
                                  QTcpServer *tcpServer;
                                  
                              protected:
                                  void incomingConnection(qintptr socketDescriptor) override;
                              
                              private:
                                  QStringList fortunes;
                                  MainWindow uicopy;
                                  
                              };
                              #endif // SERVER_H
                              
                              
                              JonBJ 1 Reply Last reply
                              0
                              • F fari35

                                @Christian-Ehrlicher @JonB yes Now its working but when I'm trying to create an object of MainWindow class in server.h class so I'm not able to create that. Cany you please tell me how can I make the object of MainWindow class?
                                server.h:

                                #ifndef SERVER_H
                                #define SERVER_H
                                #include <QStringList>
                                #include <QTcpServer>
                                #include "mainwindow.h"
                                class server : public QTcpServer
                                {
                                    Q_OBJECT
                                
                                public:
                                    server(QObject *parent = nullptr);
                                    QTcpServer *tcpServer;
                                    
                                protected:
                                    void incomingConnection(qintptr socketDescriptor) override;
                                
                                private:
                                    QStringList fortunes;
                                    MainWindow uicopy;
                                    
                                };
                                #endif // SERVER_H
                                
                                
                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by
                                #16

                                @fari35
                                You make a MainWindow object/instance by MainWindow instance or MainWindow *istance = new MainWindow.

                                when I'm trying to create an object of MainWindow class in server.h class

                                You absolutely do not want to create, or reference, any MainWindow class/object, or for that matter any UI object, in your server.h/.cpp files, which should solely work on QTcp... stuff! And nor should it attempt to #include "mainwindow.h"....

                                You need to learn to keep your classes separate, each one only doing what it needs for its own specialized functionality.

                                If, say, you do have a QMainWindow in an application using QTcpServer, mainwindow can know about server, but not the other way round. MainWindow can call methods in QTcpServer, but not the other way round. Or, they can use signals/slots if required to communicate.

                                1 Reply Last reply
                                4

                                • Login

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