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. QThread inheritance then invoking.
Qt 6.11 is out! See what's new in the release blog

QThread inheritance then invoking.

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 6.1k 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.
  • R Offline
    R Offline
    raketmus
    wrote on last edited by
    #6

    @Christian-Ehrlicher said in QThread inhertance then invoking.:

    There is a good example on how to do threading with QTcpSockets - follow that.

    I have done so but this example features no calling from the gui thread to the other thread i.e no interactivitie all is done in the thread.

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

      To pass data between threads in Qt the safest way is to use Signals and Slots

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

      1 Reply Last reply
      1
      • R Offline
        R Offline
        raketmus
        wrote on last edited by
        #8

        @Christian-Ehrlicher said in QThread inhertance then invoking.:

        To pass data between threads in Qt the safest way is to use Signals and Slots

        But this still gets called from my gui thread as the context and not SocketThread.
        How do i make the send methoed get called from SocketThread?

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

          @raketmus said in QThread inhertance then invoking.:

          But this still gets called from my gui thread as the context and not SocketThread.

          You have to trigger something in the gui thread, yes.

          And Qt sockets are async - no need to use threads at all.

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raketmus
            wrote on last edited by
            #10

            @Christian-Ehrlicher said in QThread inhertance then invoking.:

            @raketmus said in QThread inhertance then invoking.:

            But this still gets called from my gui thread as the context and not SocketThread.

            You have to trigger something in the gui thread, yes.

            And Qt sockets are async - no need to use threads at all.

            That does not explain anything and i as i have stated before i need multithreading a single thread simply cannot do.
            And my question was how do i call this from socketthread when i click and button in my gui?

            jsulmJ 1 Reply Last reply
            0
            • R raketmus

              @Christian-Ehrlicher said in QThread inhertance then invoking.:

              @raketmus said in QThread inhertance then invoking.:

              But this still gets called from my gui thread as the context and not SocketThread.

              You have to trigger something in the gui thread, yes.

              And Qt sockets are async - no need to use threads at all.

              That does not explain anything and i as i have stated before i need multithreading a single thread simply cannot do.
              And my question was how do i call this from socketthread when i click and button in my gui?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @raketmus said in QThread inhertance then invoking.:

              i need multithreading a single thread simply cannot do

              It can, Qt sockets are asynchronous - they do NOT block the thread.

              "And my question was how do i call this from socketthread when i click and button in my gui?" - emit a signal when the button is clicked. Connect a slot to that signal in your thread. By default slot is executed in the thread when doing signals/slots between threads (queued connection).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply
              1
              • jsulmJ jsulm

                @raketmus said in QThread inhertance then invoking.:

                i need multithreading a single thread simply cannot do

                It can, Qt sockets are asynchronous - they do NOT block the thread.

                "And my question was how do i call this from socketthread when i click and button in my gui?" - emit a signal when the button is clicked. Connect a slot to that signal in your thread. By default slot is executed in the thread when doing signals/slots between threads (queued connection).

                R Offline
                R Offline
                raketmus
                wrote on last edited by
                #12

                @jsulm said in QThread inhertance then invoking.:

                @raketmus said in QThread inhertance then invoking.:

                i need multithreading a single thread simply cannot do

                It can, Qt sockets are asynchronous - they do NOT block the thread.

                "And my question was how do i call this from socketthread when i click and button in my gui?" - emit a signal when the button is clicked. Connect a slot to that signal in your thread. By default slot is executed in the thread when doing signals/slots between threads (queued connection).

                Well such a system is not very good reading data that is size tagged i.e 4 bytes to int of size then you read that many bytes for a packet.
                That would simple not work as that is blocking and i dont know how not make it work like that.

                jsulmJ JonBJ 2 Replies Last reply
                0
                • R raketmus

                  @jsulm said in QThread inhertance then invoking.:

                  @raketmus said in QThread inhertance then invoking.:

                  i need multithreading a single thread simply cannot do

                  It can, Qt sockets are asynchronous - they do NOT block the thread.

                  "And my question was how do i call this from socketthread when i click and button in my gui?" - emit a signal when the button is clicked. Connect a slot to that signal in your thread. By default slot is executed in the thread when doing signals/slots between threads (queued connection).

                  Well such a system is not very good reading data that is size tagged i.e 4 bytes to int of size then you read that many bytes for a packet.
                  That would simple not work as that is blocking and i dont know how not make it work like that.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @raketmus said in QThread inhertance then invoking.:

                  That would simple not work as that is blocking

                  What is blocking?
                  You simply use readyRead signal and read until you got whole package, there is nothing blocking...
                  I suggest you take a look at Qt networking examples.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  5
                  • R raketmus

                    @jsulm said in QThread inhertance then invoking.:

                    @raketmus said in QThread inhertance then invoking.:

                    i need multithreading a single thread simply cannot do

                    It can, Qt sockets are asynchronous - they do NOT block the thread.

                    "And my question was how do i call this from socketthread when i click and button in my gui?" - emit a signal when the button is clicked. Connect a slot to that signal in your thread. By default slot is executed in the thread when doing signals/slots between threads (queued connection).

                    Well such a system is not very good reading data that is size tagged i.e 4 bytes to int of size then you read that many bytes for a packet.
                    That would simple not work as that is blocking and i dont know how not make it work like that.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #14

                    @raketmus
                    I'm trying to guess what you are saying here.

                    I think you are talking about: you read data sent across a socket. You receive, for example, a "message" which says the next thing is a 4-byte-integer, and then you receive the 4-byte integer. You presently have blocking code which says after receiving the indicator that what follows is 4 bytes, you then block on a "read 4 bytes" call. Is that about right?

                    Don't do it this way! When bytes arrive (in readRead()) append them to a buffer of pending data. Do not block! Then, you inspect the length of the buffer to see whether you have received at least as many bytes as a "message", e.g. the 4 bytes after the "indicator" here. Then you pull out the indicator plus the 4 byte integer, all already received, and process that "message".

                    This is the right way to do things in an asynchronous framework, and avoids any blocking, and need for threads. See also QDataStream Using Read Transactions.

                    1 Reply Last reply
                    1
                    • R Offline
                      R Offline
                      raketmus
                      wrote on last edited by
                      #15

                      @JonB said in QThread inhertance then invoking.:

                      @raketmus
                      I'm trying to guess what you are saying here.

                      I think you are talking about: you read data sent across a socket. You receive, for example, a "message" which says the next thing is a 4-byte-integer, and then you receive the 4-byte integer. You presently have blocking code which says after receiving the indicator that what follows is 4 bytes, you then block on a "read 4 bytes" call. Is that about right?

                      Don't do it this way! When bytes arrive (in readRead()) append them to a buffer of pending data. Do not block! Then, you inspect the length of the buffer to see whether you have received at least as many bytes as a "message", e.g. the 4 bytes after the "indicator" here. Then you pull out the indicator plus the 4 byte integer, all already received, and process that "message".

                      This is the right way to do things in an asynchronous framework, and avoids any blocking, and need for threads. See also QDataStream Using Read Transactions.

                      1. The issue here is using only 1 thread will cause lag seeing as proccesing data from 20 clients in a single thread and also doing gui will casuse lag.
                      2. I dont want async i want sync becuase my client is sync and i would like to be able to just copy code.

                      @jsulm said in QThread inhertance then invoking.:

                      @raketmus said in QThread inhertance then invoking.:

                      That would simple not work as that is blocking

                      What is blocking?
                      You simply use readyRead signal and read until you got whole package, there is nothing blocking...
                      I suggest you take a look at Qt networking examples.

                      The samples are simpley to basic non of then are able to recive binary.
                      Becuase when sending and reciveing binary you have to send how long the data is gonna be.

                      KroMignonK jsulmJ 2 Replies Last reply
                      0
                      • R raketmus

                        @JonB said in QThread inhertance then invoking.:

                        @raketmus
                        I'm trying to guess what you are saying here.

                        I think you are talking about: you read data sent across a socket. You receive, for example, a "message" which says the next thing is a 4-byte-integer, and then you receive the 4-byte integer. You presently have blocking code which says after receiving the indicator that what follows is 4 bytes, you then block on a "read 4 bytes" call. Is that about right?

                        Don't do it this way! When bytes arrive (in readRead()) append them to a buffer of pending data. Do not block! Then, you inspect the length of the buffer to see whether you have received at least as many bytes as a "message", e.g. the 4 bytes after the "indicator" here. Then you pull out the indicator plus the 4 byte integer, all already received, and process that "message".

                        This is the right way to do things in an asynchronous framework, and avoids any blocking, and need for threads. See also QDataStream Using Read Transactions.

                        1. The issue here is using only 1 thread will cause lag seeing as proccesing data from 20 clients in a single thread and also doing gui will casuse lag.
                        2. I dont want async i want sync becuase my client is sync and i would like to be able to just copy code.

                        @jsulm said in QThread inhertance then invoking.:

                        @raketmus said in QThread inhertance then invoking.:

                        That would simple not work as that is blocking

                        What is blocking?
                        You simply use readyRead signal and read until you got whole package, there is nothing blocking...
                        I suggest you take a look at Qt networking examples.

                        The samples are simpley to basic non of then are able to recive binary.
                        Becuase when sending and reciveing binary you have to send how long the data is gonna be.

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #16

                        @raketmus said in QThread inhertance then invoking.:

                        I dont want async i want sync becuase my client is sync and i would like to be able to just copy code.

                        Sorry but this don't make sense to me.
                        TCP is a stream connection, so you don't known when all data are received, you are always inform about new data received.
                        So do it synchronously or asynchronously is just a matter of "feeling".

                        And using synchronous TCP transfer with Qt is a very bad idea, you are locking a thread/wasting CPU and memory for nothing.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        1 Reply Last reply
                        5
                        • R raketmus

                          @JonB said in QThread inhertance then invoking.:

                          @raketmus
                          I'm trying to guess what you are saying here.

                          I think you are talking about: you read data sent across a socket. You receive, for example, a "message" which says the next thing is a 4-byte-integer, and then you receive the 4-byte integer. You presently have blocking code which says after receiving the indicator that what follows is 4 bytes, you then block on a "read 4 bytes" call. Is that about right?

                          Don't do it this way! When bytes arrive (in readRead()) append them to a buffer of pending data. Do not block! Then, you inspect the length of the buffer to see whether you have received at least as many bytes as a "message", e.g. the 4 bytes after the "indicator" here. Then you pull out the indicator plus the 4 byte integer, all already received, and process that "message".

                          This is the right way to do things in an asynchronous framework, and avoids any blocking, and need for threads. See also QDataStream Using Read Transactions.

                          1. The issue here is using only 1 thread will cause lag seeing as proccesing data from 20 clients in a single thread and also doing gui will casuse lag.
                          2. I dont want async i want sync becuase my client is sync and i would like to be able to just copy code.

                          @jsulm said in QThread inhertance then invoking.:

                          @raketmus said in QThread inhertance then invoking.:

                          That would simple not work as that is blocking

                          What is blocking?
                          You simply use readyRead signal and read until you got whole package, there is nothing blocking...
                          I suggest you take a look at Qt networking examples.

                          The samples are simpley to basic non of then are able to recive binary.
                          Becuase when sending and reciveing binary you have to send how long the data is gonna be.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          @raketmus said in QThread inhertance then invoking.:

                          Becuase when sending and reciveing binary you have to send how long the data is gonna be

                          And what is the problem? You need a some kind of protokol, like: send the length and then actual data. Then you receive the data asynchronously you accumulate the data in a buffer until you got the whole data (#bytes=length). This is how it works...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          4
                          • R Offline
                            R Offline
                            raketmus
                            wrote on last edited by
                            #18

                            Okay i think i have gotten it down i have switched to async issue is i had made a attempt at async eairlier but it was very bad but i have imporved it alot

                            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