Results 1 to 20 of 154

Thread: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    "Trivial to do with a signal from the thread connected to a slot in the label" -- it is not so trivial for me.
    I have looked for signals yesterday, and saw such sample
    ...Thread.h
    signals: void signalmethod();
    public: run();
    then in thread.cpp
    Thread::run() {
    signalmethod();
    } but I did it with static method. But here is non-static one?? what kind (how it should look like) of signal I shpuld use?
    2. what 2 last point?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    Quote Originally Posted by artt View Post
    I have looked for signals yesterday, and saw such sample
    Right, just declare a signal and emit it in run.
    The code creating the thread object can then connec this signal to its own slot or to the slot of another class.

    E.g. if you had a signal like this

    Qt Code:
    1. signals:
    2. void updateText(const QString &text);
    To copy to clipboard, switch view to plain text mode 
    one could directly connect it to a label that should show that text
    Qt Code:
    1. connect(thread, SIGNAL(updateText(QString)), label, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by artt View Post
    2. what 2 last point?
    Quote Originally Posted by anda_skoa View Post
    - don't call any methods of Lister in the threads
    - put code that you want the threads to execute into the thread classes

  3. #3
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    connect(thread, SIGNAL (update(QString)...) -- then I need to make the Qtextedit (Qlabel) field the member of Thread class -but other Qbuttons will be the Lister members - as well as the layout widget...
    But it need to be composite (sorry - it should call - solid or indivisable to be more apt), how one widgets would be in one gui, the others - in another??
    Last edited by artt; 1st January 2016 at 18:41.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    Quote Originally Posted by artt View Post
    connect(thread, SIGNAL (update(QString)...) -- then I need to make the Qtextedit (Qlabel) field the member of Thread class
    Nope.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    So how? In updatetext() I need to use Qtextedit field, and how I use this field without Lister object creation, or I shoud create Qtextedit in Thread object - but it shoud be in GUI-thread (to avoid ASSERT failure).
    I did not used sognal emitting never.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    The thread emits the signal, the widget receives it.
    You connect in Lister, when you've created the thread instance, before starting it.

    So the widget can either be Lister itself or one of its members.
    No need to have a member in the thread.

    Emitting the signal is just
    Qt Code:
    1. emit updateText(someQStringVariable);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    How can I use QTextEdit textview in signal without creating Lister object, as it is the part of Lister class?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    You either connect to a slot of the text edit itself or to a slot in Lister and the call setter methods on the text edit from that slot.

    Cheers,
    _

  9. #9
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    I cannot suggest how? Would you? Either way I need Lister->textview or the most simple Lister::textedit..

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    Quote Originally Posted by artt View Post
    I cannot suggest how? Would you? Either way I need Lister->textview or the most simple Lister::textedit..
    Lets assume the slot is a method of Lister.
    Then you can just address any member with its name.
    Just as you would in Java.

    Cheers,
    _

  11. #11
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    What kind of signal should be- declaration and definition; Anyway it should be connected with the button - renderdirectly (or from file) - so I need anyway the object of Lister.
    So if I add signal I need Lister object - so task appear more complex - and Slot is also Lister function and need its object if non-static.

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    Quote Originally Posted by artt View Post
    What kind of signal should be- declaration and definition
    Comment #100 has an example for a declaration.
    The definition of a signal is handled by MOC, nothing to do for you.

    Quote Originally Posted by artt View Post
    Anyway it should be connected with the button
    QPushButton has a setText slot, so that would work just like for a label.

    Quote Originally Posted by artt View Post
    So if I add signal I need Lister object
    Not sure what you mean.
    You don't need a Lister object to add a signal to the thread class.

    You don't need a Lister object to connect if there is some place other than Lister that knows both the thread object and the receiver object.
    But Lister should have both, so it is the best place to put the connect into.
    And you already have an instance of Lister, so no new thing required.

    Quote Originally Posted by artt View Post
    and Slot is also Lister function
    Not necessarily as you can connect to any other object that has a slot matching the signal.

    However, since your target for the signal seems to change a lot (first you had a label, then a text edit, now you've asked about a button), it might be easier to just connect to a slot in Lister and handle whatever UI update from there.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 1st April 2014, 08:48
  2. Destruction in separate threads
    By KevinKnowles in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2012, 09:49
  3. Problem with QProcess in two separate threads
    By viheaho in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 22:52
  4. Replies: 1
    Last Post: 7th December 2009, 07:26
  5. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 08:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.