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
    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
    So I see just transient variable, that got qtext variable after thread processing, and then assigning it to Qtextview in connect() statement by button
    A connect doesn't assign anything.
    It provides a connection between a signal and a slot.
    When the signal is emitted, the slot is invoked.
    If the signal carries arguments, then the slot is called with these arguments.

    Quote Originally Posted by artt View Post
    So my SLOT(readxmlinthread()) would like as:
    readxmlinthread() {
    thread.start();//process to xml Qstring listed2
    textview.setText(Lister::listed2);
    //use destructor to free listed2 on heap;
    }
    That is obviously not going to work, as the thread is running in parallel so that variable might or might not have been set at the point of reading it.
    Also obviously not thread-safe.

    Hence the suggetion to use a signal:
    - is thread-safe
    - sets the widget's value then the value has been calculated
    - avoids hacks such as this static variable

    Cheers,
    _

  2. #2
    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 see that it doesnt work - but I ask probably 7-8 time here - how connect Button, signal from thread with xmlprocessing results and textview it main gui? i have no suggestion as there is nessecity of transient variable.

  3. #3
    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 see that it doesnt work - but I ask probably 7-8 time here - how connect Button, signal from thread with xmlprocessing results and textview it main gui?
    So what is the problem with the examples that were already given?

    Comment #7 shows how to connect a button to a slot from within the receiver class.
    Comment #13 shows a connect of a button signal to a slot from outside both sender and receiver.
    Comment #44 has several examples of connecting signals that carry arguments.
    Comment #100 shows a declaration of a custom signal and how to use it as a cross-thread data exchange mechanism.
    Comment #104 shows how to emit such a signal.

    Cheers,
    _

  4. #4
    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 have looked through your references: but if signal() is just declared, not defined what is the sense of its argument.
    If I declare:
    signals: setuptext(Qstring);
    and then emit...
    How I can link void readxmldirectly() [that shapes these mega QString as the result of xml processing] to this signal?
    How I can link as well button with clicked() signal??
    Maybe it is impossible to realize such option -- maybe it needs some queue or some nested signal-slot connection?
    As you cannot provide some blueprint, I think you have no idea too, or I am not correct -- as the task not simply connect the signal with slot, but connect signal from button with the output of thread processing to slot??
    Maybe I could override in such way -- button,clicked(Qstring) and clicked is triggered when I emit signal with the thread::run() output?

  5. #5
    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 button's clicked() signal gets connected to a slot.
    The slot creates the thread instance, connects the thread's signal and starts the thread.
    The thread emits the signal when it is done.

    Cheers,
    _

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

    void Lister::readthread() {
    //RThread* R=new RThread();
    //R->start();
    textview->setText(Lister:ath0);
    textview->setText("Lister::listed2");
    } -- And returning to my last SLOT -- it also do not work with path0, so the issue is not in thread Rthread as the path0i defined... Maybe I need use there signal finished() of Qthread as it would be like join() in Java, then I can assign listed2 variable, but textview->setText(Lister:ath0) with predefined do not work.
    So I need connect(button,clicked(), f, readthreadxml());
    Inside readthreadxml() i need use another slot()? Or how -- but it should emit some signal
    connecting with some event --should it be the craetion of Lister f object or start of application?
    ...The thread emits the signal when it is done -- shoukld this signal to be void without nothing or it should be the readxnldirectly() content?

  7. #7
    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
    So I need connect(button,clicked(), f, readthreadxml());
    Yes

    Quote Originally Posted by artt View Post
    Inside readthreadxml() i need use another slot()?
    Inside that method you have another connect().
    Whether you connect to a slot in Lister or directly to the textview is up to you.

    Quote Originally Posted by artt View Post
    Or how -- but it should emit some signal
    No, the thread emits the signal when it has finished creating the content you want to show

    Quote Originally Posted by artt View Post
    ...The thread emits the signal when it is done -- shoukld this signal to be void without nothing or it should be the readxnldirectly() content?
    Well, it is easier if the signal transports the content because then you can directly connect to textview.

    Cheers,
    _

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

    "Inside that method you have another connect() -- I do understand as slot without connect() nothing means.
    Whether you connect to a slot in Lister or directly to the textview is up to you."
    --Indeed I need to define the slot of nested connect() in Lister.cpp, so I need to create Lister object again(or beforehand to be strict), and it will be out of created in main() so it would be the error as ever, or I need static slot with static textview -that is impossible.
    Ot I should simply use textview->setText(...), no Lister->textview->setText(...).
    Anyway --there is build in finished() signal in qthread as I wrote early, maybe it would be option.
    And I do not understand why
    Qt Code:
    1. void Lister::readthread() {
    2. textview->setText(Lister:path0);
    3. textview->setText("Lister::listed2");
    4. }
    To copy to clipboard, switch view to plain text mode 
    do not render path0 that is not empty and predefined in code. If it would work maybe I would check my previous option
    Last edited by artt; 6th January 2016 at 09:12.

  9. #9
    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
    --Indeed I need to define the slot of nested connect() in Lister.cpp, so I need to create Lister object again
    Why would you need another instance of Lister?

    Quote Originally Posted by artt View Post
    Anyway --there is build in finished() signal in qthread as I wrote early, maybe it would be option.
    Of course.
    You could also store the result in a member of the thread and retrieve it from the slot connected to finished().

    Quote Originally Posted by artt View Post
    And I do not understand why
    Qt Code:
    1. void Lister::readthread() {
    2. textview->setText(Lister:path0);
    3. textview->setText("Lister::listed2");
    4. }
    To copy to clipboard, switch view to plain text mode 
    do not render path0 that is not empty and predefined in code. If it would work maybe I would check my previous option
    That should always display "Lister::listed2", as this is the value that is set on the textview.

    Cheers,
    _

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

    Qt Code:
    1. void Lister::readthread() {
    2. textview->setText("Lister::listed2");
    3. textview->setText(Lister:path0); //In this case 2nd line render but no first one, so the first line do not //render anyway
    4. }
    To copy to clipboard, switch view to plain text mode 
    I have found the solution but not very fine as it works but for big folders like C:\Windows it use too much memory for the whole Windows XP system.
    Qt Code:
    1. void Lister::slot() {
    2. textview->setText("Lister::listed2");
    3. textview->setText(Lister::listed2);
    4. }
    5.  
    6. void Lister::readthread() {
    7. RThread* R=new RThread();
    8. R->start();
    9. QObject::connect(R, SIGNAL(finished()),this, SLOT(slot()));
    10. //textview->setText("Lister::listed2");
    11. //textview->setText(Lister::listed2);
    12. }
    To copy to clipboard, switch view to plain text mode 
    -- So I can do the conclusion about inefficiency of using threads for rendering a big text areas to Qtextedit.

  11. #11
    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 probably want to connect before you start the thread.
    Otherwise the thread could be finished before you establish the connection.

    Cheers,
    _

  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)

    No it render small folder xml immediately, but for big folder I need wait about minute to see the xml in textview, despite hunging began before it, and after rendering continues and deteriorate.
    QObject::connect(R->start(), SIGNAL(finished()),this, SLOT(slot())); using R->start() instead R lead to compile error.

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.