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. work with input/output of Thread
Forum Updated to NodeBB v4.3 + New Features

work with input/output of Thread

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 3.3k Views 3 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.
  • Venkatesh VV Offline
    Venkatesh VV Offline
    Venkatesh V
    wrote on last edited by
    #2

    Hi,
    Before start thread pass that variable to some other method of thread class and store that variable in some member variable, after that start the thread in same method

    //in .h
    QString uiData;

    //in thread class declare a method,
    void uiData(Qstring str)
    {
    uiData = str;
    this->start();
    }

    1 Reply Last reply
    2
    • A Alexanov

      Hi

      I derive a class from QThread and implement the run() method Then use Thread->start() to run it.

      How can i send variable that took from textedit (in GUI or variable in other thread ) and do something in Thread and then return the other variable (to the main thread)

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

      @Alexanov Use signals/slots to exchange data

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

      A 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Alexanov Use signals/slots to exchange data

        A Offline
        A Offline
        Alexanov
        wrote on last edited by
        #4

        @jsulm
        can you show me how can I exchange data with signal/ slot?(simple pseudo code is enough )

        jsulmJ 1 Reply Last reply
        0
        • A Alexanov

          Hi

          I derive a class from QThread and implement the run() method Then use Thread->start() to run it.

          How can i send variable that took from textedit (in GUI or variable in other thread ) and do something in Thread and then return the other variable (to the main thread)

          U Offline
          U Offline
          user23486
          wrote on last edited by
          #5

          I guess I would do it, just as with the IODevice interfcae.

          1 Reply Last reply
          0
          • A Alexanov

            Hi

            I derive a class from QThread and implement the run() method Then use Thread->start() to run it.

            How can i send variable that took from textedit (in GUI or variable in other thread ) and do something in Thread and then return the other variable (to the main thread)

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

            @Alexanov said in work with input/output of Thread:

            I derive a class from QThread and implement the run() method Then use Thread->start() to run it.

            If you don't have an event loop in that thread you can't have slots executed in it. Either move to the worker object approach or synchronize everything manually with mutexes.

            Read and abide by the Qt Code of Conduct

            A 1 Reply Last reply
            3
            • A Alexanov

              @jsulm
              can you show me how can I exchange data with signal/ slot?(simple pseudo code is enough )

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

              @Alexanov http://doc.qt.io/qt-5.9/signalsandslots.html

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

              A 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Alexanov http://doc.qt.io/qt-5.9/signalsandslots.html

                A Offline
                A Offline
                Alexanov
                wrote on last edited by
                #8

                @jsulm
                I knew Signals and slots working algorithm and how use in my code. :)
                My question is : How can send variables to main.cpp (GUI Thread Class) when emit Signal in my MyThread.cpp class ??and vice versa how can i send variable to MyThread.cpp with Signal/Slot?

                1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @Alexanov said in work with input/output of Thread:

                  I derive a class from QThread and implement the run() method Then use Thread->start() to run it.

                  If you don't have an event loop in that thread you can't have slots executed in it. Either move to the worker object approach or synchronize everything manually with mutexes.

                  A Offline
                  A Offline
                  Alexanov
                  wrote on last edited by
                  #9

                  @kshegunov
                  you say for working with variable (send/receive) , is better to use worker object approach or synchronize everything manually with mutexes?

                  In worker thread object can i send or receive between classes? (can you show me how ? with simple example?)
                  I need very easy solution to send or receive variable with thread ?(in C# language it was very easy but i did not know why in Qt in c language is very complex :(

                  kshegunovK 1 Reply Last reply
                  0
                  • A Alexanov

                    @kshegunov
                    you say for working with variable (send/receive) , is better to use worker object approach or synchronize everything manually with mutexes?

                    In worker thread object can i send or receive between classes? (can you show me how ? with simple example?)
                    I need very easy solution to send or receive variable with thread ?(in C# language it was very easy but i did not know why in Qt in c language is very complex :(

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

                    @Alexanov said in work with input/output of Thread:

                    you say for working with variable (send/receive) , is better to use worker object approach or synchronize everything manually with mutexes?

                    I didn't say any one is better, I just presented you with your two options. To have slots executed in a thread it has to have an event loop, which means you need to use a worker object. The other way to do it is to make your class(es) thread safe by manually synchronizing the concurrent access to the variables that are shared - through mutexes and/or semaphores.

                    In worker thread object can i send or receive between classes?

                    Yes, with signals and slots. All the examples you need are in the documentation and here in the forum, please search for them.

                    I need very easy solution to send or receive variable with thread ?(in C# language it was very easy but i did not know why in Qt in c language is very complex :(

                    There's no "very easy" when one talks about threading (and parallelism in general), there are no real "quick fixes", sorry. I don't know C# so I don't have any idea how easy it is, but C++ is a complex language with many, many, many pitfalls and nuances. Qt has nothing to do with it all, it's just a library that provides great convenience, but it will not do the work for you.

                    Read and abide by the Qt Code of Conduct

                    A 1 Reply Last reply
                    3
                    • kshegunovK kshegunov

                      @Alexanov said in work with input/output of Thread:

                      you say for working with variable (send/receive) , is better to use worker object approach or synchronize everything manually with mutexes?

                      I didn't say any one is better, I just presented you with your two options. To have slots executed in a thread it has to have an event loop, which means you need to use a worker object. The other way to do it is to make your class(es) thread safe by manually synchronizing the concurrent access to the variables that are shared - through mutexes and/or semaphores.

                      In worker thread object can i send or receive between classes?

                      Yes, with signals and slots. All the examples you need are in the documentation and here in the forum, please search for them.

                      I need very easy solution to send or receive variable with thread ?(in C# language it was very easy but i did not know why in Qt in c language is very complex :(

                      There's no "very easy" when one talks about threading (and parallelism in general), there are no real "quick fixes", sorry. I don't know C# so I don't have any idea how easy it is, but C++ is a complex language with many, many, many pitfalls and nuances. Qt has nothing to do with it all, it's just a library that provides great convenience, but it will not do the work for you.

                      A Offline
                      A Offline
                      Alexanov
                      wrote on last edited by
                      #11

                      @kshegunov said in work with input/output of Thread:

                      examples

                      Thank you very much .....
                      I worked with signal /slot to transfer data between thread .

                      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