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. GUI based Threaded Socket Programming

GUI based Threaded Socket Programming

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.4k 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.
  • M Offline
    M Offline
    MohammadReza
    wrote on 12 Apr 2017, 19:56 last edited by
    #1

    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?

    K 1 Reply Last reply 12 Apr 2017, 21:20
    0
    • M MohammadReza
      12 Apr 2017, 19:56

      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?

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 12 Apr 2017, 21:20 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 13 Apr 2017, 02:06 last edited by
        #3

        Thanks for your reply.

        Can you explain your solution in code in detail?

        Thanks in advance.

        K 1 Reply Last reply 13 Apr 2017, 12:31
        0
        • B Offline
          B Offline
          BjornW
          wrote on 13 Apr 2017, 06:49 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 13 Apr 2017, 17:55
          5
          • M MohammadReza
            13 Apr 2017, 02:06

            Thanks for your reply.

            Can you explain your solution in code in detail?

            Thanks in advance.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 13 Apr 2017, 12:31 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
            • B BjornW
              13 Apr 2017, 06:49

              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 13 Apr 2017, 17:55 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 Apr 2017, 21:51 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 14 Apr 2017, 01:54
                3
                • S SGaist
                  13 Apr 2017, 21:51

                  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 14 Apr 2017, 01:54 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 14 Apr 2017, 20:48 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 15 Apr 2017, 14:33
                    0
                    • S SGaist
                      14 Apr 2017, 20:48

                      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 15 Apr 2017, 14:33 last edited by
                      #10

                      @SGaist
                      Yes you are right.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 15 Apr 2017, 17:07 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

                        7/11

                        13 Apr 2017, 21:51

                        • Login

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