Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. GUI based Threaded Socket Programming
Qt 6.12 LTS is out! See what's new in the release blog

GUI based Threaded Socket Programming

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 6.0k Views 2 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.
  • M MohammadReza

    Hello dears,

    I use this code in a console application:
    http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php

    I want to use this code in an GUI Based app.
    The prblem is MainWindow class inheres from QMainWindow. It`s impossible:

    class MainWindow : public QMainWindow , public QTcpServer
    

    How can I solve this problem?

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #2

    @MohammadReza said in GUI based Threaded Socket Programming:

    How can I solve this problem?

    By not deriving twice from QObject. Derive from the QTcpServer class in a dedicated class of your own and aggregate an instance of it in your main window.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    4
    • M Offline
      M Offline
      MohammadReza
      wrote on last edited by
      #3

      Thanks for your reply.

      Can you explain your solution in code in detail?

      Thanks in advance.

      kshegunovK 1 Reply Last reply
      0
      • BjornWB Offline
        BjornWB Offline
        BjornW
        wrote on last edited by BjornW
        #4

        Do not create a main window that inherits from QTcpServer, it is bad design :/.

        EDIT:

        For example, it's better to do

        class MainWindow : public QMainWindow
        {
        public:
           MainWindow();
        private:
           QTcpServer* m_pServer;
        };
        
        ...
        
        MainWindow::MainWindow()
           : QMainWindow()
           , m_pServer(new QTcpServer(this))
        {
           //Connect relevant signals...
        }
        

        Altough I would prefer even more separation between TCP stuff and GUI stuff.

        M 1 Reply Last reply
        5
        • M MohammadReza

          Thanks for your reply.

          Can you explain your solution in code in detail?

          Thanks in advance.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #5

          @MohammadReza said in GUI based Threaded Socket Programming:

          Can you explain your solution in code in detail?

          More or less I'd had in mind what Bjorn gave as an example.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • BjornWB BjornW

            Do not create a main window that inherits from QTcpServer, it is bad design :/.

            EDIT:

            For example, it's better to do

            class MainWindow : public QMainWindow
            {
            public:
               MainWindow();
            private:
               QTcpServer* m_pServer;
            };
            
            ...
            
            MainWindow::MainWindow()
               : QMainWindow()
               , m_pServer(new QTcpServer(this))
            {
               //Connect relevant signals...
            }
            

            Altough I would prefer even more separation between TCP stuff and GUI stuff.

            M Offline
            M Offline
            MohammadReza
            wrote on last edited by
            #6

            @BjornW
            Thanks @BjornW
            But I want to re-implement incomingConnection. So main window must inherits from QTcpServer.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #7

              Hi,

              Then create a class based on QTcpServer that handles the network stuff the way you want it. There's really no sense in having a widget inherit QTcpServer.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              3
              • SGaistS SGaist

                Hi,

                Then create a class based on QTcpServer that handles the network stuff the way you want it. There's really no sense in having a widget inherit QTcpServer.

                M Offline
                M Offline
                MohammadReza
                wrote on last edited by
                #8

                @SGaist
                Thanks,
                Yes, you are right.
                But I want to send message to the client via a button and textbox in QMainWindows but I don`t have any access to created socketDescriptor in MyThread class to send my message.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  It seems you are trying to make a GUI on top of the server rather than a client for your server, isn't it ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    It seems you are trying to make a GUI on top of the server rather than a client for your server, isn't it ?

                    M Offline
                    M Offline
                    MohammadReza
                    wrote on last edited by
                    #10

                    @SGaist
                    Yes you are right.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Then keep things cleanly separated. You are trying to make a super class that does everything and that's a bad idea.

                      By the way, why the GUI for the server rather than the clients ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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
                      • Unsolved