Page 7 of 8 FirstFirst ... 5678 LastLast
Results 121 to 140 of 154

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

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

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

  3. #123
    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;
    }

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

  5. #125
    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,
    _

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

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

  8. #128
    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?

  9. #129
    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,
    _

  10. #130
    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?

  11. #131
    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,
    _

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

  13. #133
    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,
    _

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

  15. #135
    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,
    _

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

  17. #137
    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
    QObject::connect(R->start(), SIGNAL(finished()),this, SLOT(slot())); using R->start() instead R lead to compile error.
    Why would you even try that?

    It has nothing to do with what I wrote.
    I pointed you to a potential race condition and suggested a fix for it.

    Cheers,
    _

  18. #138
    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)

    Why do you state that I connect before the start of thread as it is obvious from that excerpt of code that R->start(); then connect(R,finished...). And it works in general but with memory leak or so. Anyway what would you suggest just with your version of emitting signal despite here is built-in finished() signal. And it seems is the simpliest way

  19. #139
    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
    Why do you state that I connect before the start of thread
    No, I said you should connect before starting.

    Quote Originally Posted by artt View Post
    as it is obvious from that excerpt of code that R->start(); then connect(R,finished...)
    Exactly.
    Start before connect, i.e. the wrong way around.

    Cheers,
    _

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

    "terminate called after throwing an instance of 'std::bad_alloc'
    what(): std::bad_alloc
    terminate called recursively..."
    if I put connect before start of thread (I need click button twice so it incorrect in principle as it was without nested concnet())
    Anyway as in other way. there is bad_alloc, despite I put Lister::listed2 on heap memory: bad_alloc is when I put in the end
    delete Lister::listed2; I need probably uqual it to Null. But in my first variant-- evrything works fine despite temporal freezing.
    But after 2-3 minutes the whole buncg of xml from WINDOWS folder is shown in textview.
    Last edited by artt; 9th January 2016 at 01:49.

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.