Page 1 of 3 123 LastLast
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 How to use separate threads for pushbuttons in QT (like SwingWorker in Java)

    I need to realize several buttons (as well as Qlabels, QtextEdit) in QWidget.
    But I need to do it non-lockable, so if it is executinh a long time, it should be not hanging.
    In java it works very fine with SwingWorker class, but not with Runnable.
    So I need to know what analog I need to use in Qt.
    Here is excerpts from java code. As it is very compact so I need to follow it in Qt but with local
    classes:
    Qt Code:
    1. class Writefile extends SwingWorker {//Declaration of custom thread class
    2. public Object doInBackground() {
    3. XMLCreator.stax();//class and method that realize XML writing to file.
    4. return null;
    5. }
    6.  
    7. writetofile.addActionListener(new ActionListener() { //adding listener to button
    8. public void actionPerformed(ActionEvent e) {
    9. //XMLCreator.stax();
    10. Writefile process = new Writefile(); //initizalization of swingworjer class
    11. process.execute(); //starting separate task in separate thread
    12. }
    13. });
    To copy to clipboard, switch view to plain text mode 
    So how should I use
    Qt Code:
    1. Qtobject::connect(writebutton,SIGNAL(clicked()), this, SLOT(Filewalker::writetofile()));
    To copy to clipboard, switch view to plain text mode 
    //The last is the slot declared in Filewalker class header
    Last edited by artt; 13th December 2015 at 18:08.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    All QWidget instances must be created and run in the main GUI thread.

    What you might be able to do is to send a signal to a different thread upon a push-button click. That is, the pushbutton click is handled in a slot in the main thread, but it sends a signal to a non-GUI handler slot in a different thread.

    Google for Wysota's excellent article on "Keeping the GUI responsive".

  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)

    There is an part about workerthread: Qthread. So I do understand that I need to extend the Qthread, put the
    Filewalker::writetofile() in run() method then just initialize Qthread2 in Qwidget in such way as
    Qthread2 writes; And then put it in connect method in slot like SLOT(writes.exec()).
    Qt Code:
    1. Qtobject::connect(writebutton,SIGNAL(clicked()), this, SLOT(Filewalker::writetofile()));
    2. Qtobject::connect(writebutton,SIGNAL(clicked()), &writes, SLOT(writes.exec());
    To copy to clipboard, switch view to plain text mode 
    If so should I use "this" in the connect 3-rd argument, or put the instance of Qthread2?
    //Despite in QAssistant the start() is prevalent, as I see, to exec().
    //There is also the question about how and where (in what file) to declare and initialize several Qthreads?
    In Java - I do it in Jframe file, could I do it in Qwidget file?

  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)

    Despite in QAssistant the start() is prevalent, as I see, to exec().
    QThread::start() starts the thread, i.e. requests a thread from the operating system and lets it execute run().

    QThread::exec() can be called from within run() to start the thread's event loop. The default implementation of QThread::run() basically does just that.

    If so should I use "this" in the connect 3-rd argument, or put the instance of Qthread2?
    The third argument of connect() is the receiver object, so it depends which object you want to execute the slot on.

    The equivalent to your Java code would be to connect to "this" and in that slot create the worker thread and start it.

    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)

    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.

  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)

    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,
    _

  7. #7
    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 

  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)

    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,
    _

  9. #9
    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.

  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 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,
    _

  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)

    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.

  12. #12
    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 do "someslot" should look like? I defined the Filewalker::walk() (as a function for indexation) as public slot in Filewalker.h - is it not enough?? And I think that all slots like walk(), writetoxmlfile() and qlist <filewalker*> should be put in Lister as class with default constructor (is it possible in such way to use class Lister: public QObject) and Filewalker as class just with fields. So I would put &f as the reference to Lister object. Then how to use UI Widget class just to put the code from current main () to some kind as class guis: public qwidget? Should I do some modification of automatic shaped .ui form file?

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    Artt - I realize that you may be a skilled Java programmer, but it is obvious from your questions that you do not know much about either C++ or Qt. You are trying to run before you even know how to crawl.

    Qt comes with many, many C++ example programs to help you learn all aspects of Qt. Many of the questions you are asking could be answered if you studied the examples and learned from them. Right now, you are asking so many questions which are so confused that it is almost impossible to know where to start to answer them.

    So open Qt Creator, click on the Welcome page and look at the Examples or the "Getting Started Programming with Qt Widgets" Tutorial. I think you will find it more helpful than trying to learn C++ and Qt programming from us.

  14. #14
    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 do "someslot" should look like? I defined the Filewalker::walk() (as a function for indexation) as public slot in Filewalker.h - is it not enough??
    Yes, that is enough.

    Quote Originally Posted by artt View Post
    And I think that all slots like walk(), writetoxmlfile() and qlist <filewalker*> should be put in Lister
    That would be ok as well.

    Quote Originally Posted by artt View Post
    as class with default constructor
    Type of constructor doesn't matter at all, no difference to Java.

    Quote Originally Posted by artt View Post
    (is it possible in such way to use class Lister: public QObject) and Filewalker as class just with fields. So I would put &f as the reference to Lister object.
    Sure, why not.

    Quote Originally Posted by artt View Post
    undefined reference to `vtable
    Make sure that the header for Lister is in the HEADERS variable in your .pro file and re-run qmake.

    Cheers,
    _

  15. #15
    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)

    Untill now the 2 headers were absent as there is 2 examples of new Lister=Filewalker combination (nested meanwhile so last one jhave no such headers as Filewalkers.cpp with definitions of slots). But after updating it and quiting-opening Qtcraetor -- the Build-Debug menu is inactive again - so I cannot check with new conditions.
    Here I provid my current pro file:
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = Filewalker2
    6. TEMPLATE = app
    7.  
    8.  
    9. SOURCES += main.cpp Filewalker.cpp filewalker2.cpp
    10.  
    11. HEADERS += filewalker2.h Lister.h Filewalker.h
    12.  
    13.  
    14. FORMS += filewalker2.ui
    To copy to clipboard, switch view to plain text mode 
    The filewalker2.h and filewalker2.cpp were created during the creation of project so could I
    delete them, how they influence the my native files? Maybe there is some bad interaction?

    #include "filewalker2.h"
    #include "ui_filewalker2.h"

    Filewalker2::Filewalker2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Filewalker2)
    {
    ui->setupUi(this);
    }

    Filewalker2::~Filewalker2()
    {
    delete ui;
    }

    and header:
    #ifndef FILEWALKER2_H
    #define FILEWALKER2_H

    #include <QWidget>

    namespace Ui {
    class Filewalker2;
    }

    class Filewalker2 : public QWidget
    {
    Q_OBJECT

    public:
    explicit Filewalker2(QWidget *parent = 0);
    ~Filewalker2();

    private:
    Ui::Filewalker2 *ui;
    };

    #endif // FILEWALKER2_H

  16. #16
    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)

    If you don't need these files, then of course you can delete them if you also remove their name occurences from the .pro file.

    Cheers,
    _

  17. #17
    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)

    Should I make start() as a slot? Or to be correctly: Should I made some thirdlevel Wrapper method for walk as slot? That is place Lister::walk() in run(), then A.start(), and them Someclass(Lister also f.e.)::walkwiththread() as slot, where I would put A.start()?// should I use event in my thread to correctly interact with Mainthread? Some kind of post...()?

  18. #18
    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
    Should I make start() as a slot?
    QThread::start() is a slot.

    Quote Originally Posted by artt View Post
    Or to be correctly: Should I made some thirdlevel Wrapper method for walk as slot? That is place Lister::walk() in run(), then A.start(), and them Someclass(Lister also f.e.)::walkwiththread() as slot, where I would put A.start()?// should I use event in my thread to correctly interact with Mainthread? Some kind of post...()?
    I don't have a clue what you mean.

    But if you meant creating a slot that will call walk with an argument, then yes.

    Cheers,
    _

  19. #19
    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 did not understand the first of your last statements? Is it some constatation(affirmation)? And I mean to make public slot: void Lister::walkwiththread() { A.start()}, when void IThread::run { Lister::walk(QString)}...
    /"Yes, that's a workable direction" -- is it not too much? Maybe there would be some cycle -- lister method --ithreadmethod--starting
    ithreadmethod--another lister method -- could it work? -- as I am really very tired of inactive Build-Debug menu
    Last edited by artt; 19th December 2015 at 17:32.

  20. #20
    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)

    Yes, that's a workable direction.

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