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)

    I though that I should do in such way(I didnot explored Qthread enough yet):
    Qt Code:
    1. class Writefile: public QThread {
    2. public void run() {
    3. Filewalker::writetoXML();
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 
    here is question is it enough just put Filewalker.h (just with Filewalker::writetoXML() declaration, but definition in main.cpp --really seems improbable, so I think I need to create header for writetoXML() --writetoXML.h ) in aforementioned file with Writefile
    then, to initialize Qthread in QWidget:
    Qt Code:
    1. Writefile wr; //(or Writefile* wr=new WriteFile();)
    2. Qtobject::connect(writebutton,SIGNAL(clicked()), this (//or &wr), SLOT(wr.exec() //or wr.start);
    To copy to clipboard, switch view to plain text mode 
    What is incorrect, it is my blueprint...

    So should I Extend MyWidget the Qobject or can I realize just with main(widget).cpp --that is--should I declare the slots in final Widget file or in initial file such as FileWalker.h
    or methodheader - writetoXML.h?

    So maybe I should abstain of this console file from here http://www.qtcentre.org/threads/6455...197#post285197 and put all definition of
    slots (that are functions from Filewalker) in one big Widget File that defines such functions as (indexate files, writetoxml, renderxml, searchbyname) with QButtons, Qlabels, and connect statements.
    The question is appered in such case -- where to define Qthread class for every button (in header?), and then initialize it in Widget file???
    Last edited by artt; 13th December 2015 at 23:47.

  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)

    here is question is it enough just put Filewalker.h (just with Filewalker::writetoXML() declaration, but definition in main.cpp
    Yes

    What is incorrect, it is my blueprint...
    The SLOT macro needs a slot signature. That is the slot's name and, if applicable, all argument types.

    should I declare the slots in final Widget file
    Well, that is a non-question, the slot needs to be a member of the receiver class, which is already declared somewhere.
    So if "this" is your receiver object, then the slot will be part of the declaration of that class.

    where to define Qthread class for every button (in header?), and then initialize it in Widget file???
    If you want to keep references to the thread(s) then yes, as a member of the class that starts them would be a good choice.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Why bother with QThreads? Just use QtConcurrent::run(). Here:
    Qt Code:
    1. ...
    2. connect(myButton, SIGNAL(clicked()), SLOT(runMyFunctionInAnotherThread()));
    3. ...
    4. void myFunction() {
    5. // This function is not meant to be run in the GUI thread
    6. }
    7. ...
    8. void MyClass::runMyFunctionInAnotherThread() {
    9. QtConcurrent::run(myFunction);
    10. }
    To copy to clipboard, switch view to plain text mode 

  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)

    QtConcurrent::run() might be a bit weird to use when the function generates a result (usage of the QFuture API).

    One also needs to be aware that the number of threads is limited, especially when using the overload that does not take a thread pool but uses the global instance.

    And at least in one occasion I had a task not run in a separate thread. Not sure if that was a bug or something like work-stealing in case of an overloaded thread pool, I don't trust it to be run separately.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    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 anda_skoa View Post
    QtConcurrent::run() might be a bit weird to use when the function generates a result (usage of the QFuture API).
    I concede that other QtConcurrent operations have a complicated way of returning results, but QtConcurrent::run() is actually fairly simple. Just set up a QFutureWatcher. When it emits finished(), you can immediately recover the result by calling result(). This is as simple as an asynchronous API can be (well, maybe a signal finished(T result) would be even simpler). A QThread-based solution will end up with a similar interface.

    Quote Originally Posted by anda_skoa View Post
    One also needs to be aware that the number of threads is limited, especially when using the overload that does not take a thread pool but uses the global instance.
    Sure, but unless fine-grained control over the threads is required (which does not seem to be a primary concern of the OP), it makes sense to let QtConcurrent adapt the number of concurrent jobs to the actual capacity of the system.

    Quote Originally Posted by anda_skoa View Post
    And at least in one occasion I had a task not run in a separate thread. Not sure if that was a bug or something like work-stealing in case of an overloaded thread pool, I don't trust it to be run separately.
    This probably is a bug; the documentation specifically mentions that the function is run in a separate thread.

    In summary, this looks to me like the standard use case for QtConcurrent::run(): the OP just needs a high-level, concise API to run a task in a separate thread, and trusts the platform with the details of job management and scheduling.

  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)

    Quote Originally Posted by yeye_olive View Post
    I concede that other QtConcurrent operations have a complicated way of returning results, but QtConcurrent::run() is actually fairly simple. Just set up a QFutureWatcher. When it emits finished(), you can immediately recover the result by calling result(). This is as simple as an asynchronous API can be (well, maybe a signal finished(T result) would be even simpler). A QThread-based solution will end up with a similar interface.
    I generally agree, but you have to know that there is QFutureWatcher and the API is a bit uncommon, even for experienced C++ developers.
    But since the OP even has difficulties with basic C++ such as heap allocation or header/source file splits, I have my doubts the QtConcurrent::run() would be understandable.

    Quote Originally Posted by yeye_olive View Post
    Sure, but unless fine-grained control over the threads is required (which does not seem to be a primary concern of the OP), it makes sense to let QtConcurrent adapt the number of concurrent jobs to the actual capacity of the system.
    Well, thread pool based tasks should be non-blocking (CPU bound), not sure if that is the case of the tasks the OP wants to run.

    Quote Originally Posted by yeye_olive View Post
    This probably is a bug; the documentation specifically mentions that the function is run in a separate thread.
    Yes, probably, or different in Qt4 which I used at that time.

    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)

    I think maybe it is better to convert my console(main.cpp) file with function definition in widget(main.cpp), or just simply copy from one to another as thelast require additional form file - as it is inevident that definition in another cpp would be workable
    in main.cpp (how can i include another cpp to main.cpp, as I do it with filewalker.h--if I include filewalker.h with function declaration - do another cpp know where to find its definition or is it present in the whole?).
    About this pattern of code that should be present probably in Widget file:
    Qt Code:
    1. connect(myButton, SIGNAL(clicked()), SLOT(runMyFunctionInAnotherThread()));
    2. ...
    3. void myFunction() {
    4. // This function is not meant to be run in the GUI thread
    5. }
    6. ...
    7. void MyClass::runMyFunctionInAnotherThread() {
    8. QtConcurrent::run(myFunction);
    9. }
    To copy to clipboard, switch view to plain text mode 
    As I understood -- MyClass shoud some kind of Qthread(if I use this threadclass) and its MyClass.h should be included in Widgetfile.
    "myFunction()" -- should be some kind as Filewalker::writexmltofile() definition?
    And void MyClass::runMyFunctionInAnotherThread() should use MyClass::startthread() when run() found in MyClass declaration.
    Where is SLOT signature here? Or it should not be declared as slot in the same file where is connect() statement? Where is 3-rd - receiving object - argument here? "this" or so?
    Last edited by artt; 14th December 2015 at 16:31.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.