Page 6 of 8 FirstFirst ... 45678 LastLast
Results 101 to 120 of 154

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

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

  2. #102
    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,
    _

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

  4. #104
    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,
    _

  5. #105
    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?

  6. #106
    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,
    _

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

  8. #108
    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,
    _

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

  10. #110
    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,
    _

  11. #111
    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)

    setText() is about adding the label for button itself- anyway it would not affect probably the Qtextedit area as it is the another widget. Anyway I need to use non-static variable of some class (in that case the Lister, or any other) that should be instatiated. If instantiated in some thread -- it will be out of non-GUI thread that lead to error.
    What signal could do - just trigger slot reaction- or how it put textstream to created Lister textview if it needs to put inside another thread first. Could your provide workable example of it, I think it should not be difficult?

  12. #112
    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
    setText() is about adding the label for button itself
    Yes, correct.

    Quote Originally Posted by artt View Post
    Anyway I need to use non-static variable of some class (in that case the Lister, or any other) that should be instatiated.
    Correct and you already have an instance of Lister.

    Quote Originally Posted by artt View Post
    If instantiated in some thread -- it will be out of non-GUI thread that lead to error.
    Exactly, so don't do that.

    Quote Originally Posted by artt View Post
    What signal could do - just trigger slot reaction- or how it put textstream to created Lister textview if it needs to put inside another thread first.
    I am afraid I have no idea what you mean.
    What do you "need to put inside another thread first"?

    Quote Originally Posted by artt View Post
    Could your provide workable example of it, I think it should not be difficult?
    A workable example for what?

    Cheers,
    _

  13. #113
    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 need the "textview" and qlabel to assigned inside thread, in another case, I should leave as it is. Whether you propose to create separate
    thread for rendering text, but Qtextedit assigned to be done outside of it. What then the sense of it? What work of such thread would do? How it would relinquish the main-gui as the asisgning should be done just there. Maybe such assigning could not lead to freezing so any sense to make new thread, despite in Java rendering of big xml files was time consuming enough?
    Then why I need to use textupdate of qpushbutton?
    Last edited by artt; 2nd January 2016 at 19:54.

  14. #114
    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 need the "textview" and qlabel to assigned inside thread
    No. Remember only the main thread interacts with widgets directly.

    You need the thread to send data via signal and the slot puts that data onto widgets.
    The given example was for text, i.e. QString.

    Cheers,
    _

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

    Lets assume that several signals could be connected to one slot -- despite it seems that it could be without signals in the whole.
    We have such method:
    Qt Code:
    1. void Lister::readxmldirectly() {
    2. QXmlStreamWriter stream(ba);
    3. stream.setAutoFormatting(true);
    4. stream.writeStartDocument();
    5. stream.writeStartElement("FILESYSTEM");
    6. foreach (Filewalker* fp, Lister::listed) {
    7. stream.writeStartElement("FILE");
    8. stream.writeStartElement("NAME");
    9. ..........
    10. QTextStream ReadFile(ba); (1)
    11.  
    12. Lister::textview->setText(ReadFile.readAll()); (2)
    13. }
    To copy to clipboard, switch view to plain text mode 

    then I could put the (1) in signal processing method --signal()
    then emit it, or simply put in run(), but in this case the signal() should be static one.
    Then pass the (1) Qtextstream already outside of thread in SLOT of Lister
    where would be the last line of above method-
    Qt Code:
    1. Lister::textview->setText(ReadFile.readAll());
    To copy to clipboard, switch view to plain text mode 
    --If use the emit signal() --where put [emit signal()]?
    Inside run()?
    And it is in case if the signature of connect would be such one,
    Qt Code:
    1. connect(f->readxml, SIGNAL(clicked()),f, SLOT(readthread()));
    To copy to clipboard, switch view to plain text mode 
    As I need connect readxml button with Slot
    --So I need put
    Qt Code:
    1. Lister::textview->setText(ReadFile.readAll())
    To copy to clipboard, switch view to plain text mode 
    just inside (readthread())??

  16. #116
    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
    Lets assume that several signals could be connected to one slot
    Yes, that is possible.

    Quote Originally Posted by artt View Post
    despite it seems that it could be without signals in the whole.
    Sure, signals are only one option of transferring data from a worker thread to the main thread.
    Usually the most convenient way though.

    Quote Originally Posted by artt View Post
    We have such method:
    Qt Code:
    1. void Lister::readxmldirectly() {
    2. QXmlStreamWriter stream(ba);
    3. stream.setAutoFormatting(true);
    4. stream.writeStartDocument();
    5. stream.writeStartElement("FILESYSTEM");
    6. foreach (Filewalker* fp, Lister::listed) {
    7. stream.writeStartElement("FILE");
    8. stream.writeStartElement("NAME");
    9. ..........
    10. QTextStream ReadFile(ba); (1)
    11.  
    12. Lister::textview->setText(ReadFile.readAll()); (2)
    13. }
    To copy to clipboard, switch view to plain text mode 
    Assuming that the last two statements are not in the loop, why not just simply let the QXmlStreamWriter work on a QString and set that on "textview"?

    Cheers,
    _

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

    Not the first statement is the whole method. The second statement is just last line. That I wanted to put just last line out of workerthread, so assignt textstream to qtextedit in GUI-thread or convert it beforehand to Qstring and then to assign just it to qtextedit. But I cannot know if I can to return method of such big qstring in about 1.5 million lines passing it to textview.setText(...)?

  18. #118
    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
    Not the first statement is the whole method. The second statement is just last line.
    Ah, ok.

    Quote Originally Posted by artt View Post
    That I wanted to put just last line out of workerthread, so assignt textstream to qtextedit in GUI-thread or convert it beforehand to Qstring and then to assign just it to qtextedit.
    Right, that would be my approach as well:
    - connect thread's signal to textview's setText()
    - create the QString in the worker thread
    - emit string via signal

    Quote Originally Posted by artt View Post
    But I cannot know if I can to return method of such big qstring in about 1.5 million lines passing it to textview.setText(...)?
    No better way than to try and find out

    Cheers,
    _

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

  20. #120
    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,
    _

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.