Page 3 of 3 FirstFirst 123
Results 41 to 60 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)

    So how I need to emit signal with run() method? Rthread::run{ Lister::renderxmlsignal(); emit renderxmlsignal();}. But how to connect 2 signals with 1 slot. With 2 connect statement, but with the same slot? The signall emitting free way could be process listed to xml and save it in operative memory in another big lister Qstring variable like listed2, and just put it textview. Anyway in such method implemetation I need 2 connect statements. Unfortunately thread.start() can just return void type, no qstring, so I need to save big qstring somewhere or use signal emitting.

  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
    So how I need to emit signal with run() method?
    See comment #104

    Quote Originally Posted by artt View Post
    But how to connect 2 signals with 1 slot. With 2 connect statement, but with the same slot?
    Yes.

    Quote Originally Posted by artt View Post
    Unfortunately thread.start() can just return void type, no qstring, so I need to save big qstring somewhere or use signal emitting.
    QThread::run() is executed in another thread, so even if it would return something your main thread would not have access to it.

    So indeed, you can either store the string and retrieve it after the thread is finished or emit it via a signal.

    Cheers,
    _

  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)

    I do not understand how link this 2 connect statements:
    f.i. connect(thread, SIGNAL(updateText(QString)), label, SLOT(setText(QString)));
    what should be (updateText(QString)) - should it be wrapper of run() or start(), why to use Qstring argument?
    I do not clearly grasp the linking of signal-slot (if button-click() --> trigger the SLOT), but here I want to signal() return the whole Qstring, then SLOT take it and assign to textview. Or you mean (updateText(QString)) is just for emit signal() so it takes the Qstring?
    Why I cannot just use (thread, SIGNAL(start(), textview, SLOT(setText()) and or Qstring argument is exact argument that link signal and slot?
    Anyway I need use button after clicking which I settext() but I cannot use it in aforementioned example.
    So if just use it -- the textview would be filled just after launching the application, but I need use it just after clicking.
    So as I understand I need some intermediate variable as listed2 where I can put the results of xml processing, and then put it in Qtextedit yet in main-gui...

  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
    I do not understand how link this 2 connect statements:
    f.i. connect(thread, SIGNAL(updateText(QString)), label, SLOT(setText(QString)));
    This is only one connect statement.

    Quote Originally Posted by artt View Post
    what should be (updateText(QString)) - should it be wrapper of run() or start()
    It is a signal, it doesn't wrap anything.

    Quote Originally Posted by artt View Post
    why to use Qstring argument?
    You want to send text from the thread to the GUI, QString is Qt's class for text.


    Quote Originally Posted by artt View Post
    Or you mean (updateText(QString)) is just for emit signal() so it takes the Qstring?
    Yes, signals are emitted.

    Quote Originally Posted by artt View Post
    Why I cannot just use (thread, SIGNAL(start(), textview, SLOT(setText())
    Who would have thought?
    Could it be that thread does not have a start() signal?
    Could it be that textview does not have a setText() slot?

    Quote Originally Posted by artt View Post
    Anyway I need use button after clicking which I settext() but I cannot use it in aforementioned example.
    If you need to set a text when a button is clicked, create a slot that you can connect to the button's signal and call widget setters from there.

    Quote Originally Posted by artt View Post
    So as I understand I need some intermediate variable as listed2 where I can put the results of xml processing, and then put it in Qtextedit yet in main-gui...
    I have no idea what you mean.

    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)

    Our last spin of discussion is probably the most worthless and long -- of course I need simple action triggeed by button.
    As all of the others. So I see just transient variable, that got qtext variable after thread processing, and then assigning it to Qtextview in connect() statement by button. Notheing more... Despite processing xml, then assigning to variable, despite using some aditional resources as virtual memory would be justified, as I just put this variable to Qtextview in readxml() and maybe free the virtual memory in the same method.
    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;
    }

  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)

    Here I defined such variable and slot method in Lister.cpp:
    Qt Code:
    1. QList <Filewalker*> Lister::listed;
    2. QString Lister::listed2("dddddd"); //I assigned such simple text for simple testing
    3. ...
    4. void Lister::readthread() { //As slot function
    5. RThread* R=new RThread();
    6. R->start();
    7. textview->setText(Lister::listed2); //It doesnt render dddddd
    8. textview->setText("Lister::listed2"); //But it render "Lister::listed2" --So did I incorrectly assign listed2 to textview with setText()?
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by artt; 4th January 2016 at 21:48.

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

  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)

    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.

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

  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)

    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?

  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)

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

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.