Page 5 of 8 FirstFirst ... 34567 ... LastLast
Results 81 to 100 of 154

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

  1. #81
    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 to resolve that fuct that SLOT-method should be related to its object in connect statement, as well I need the object in run() method to call this method in separate thread?? Now I have the yellow background(and do not know why?) around the static Qtextedit variable when assignong to it in such kind as Lister::textview->

  2. #82
    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)

    Sorry, I can't follow.
    What is the problem?

    Cheers,
    _

  3. #83
    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 try to define such textedit:
    Lister.h:
    static QTextEdit* textview;
    In Lister.cpp:
    //Lister::textview = new QTextEdit(); -- in this case I got yellow background. So error.
    If textview=new QTextEdit() - just if non-static.
    So I try to make qtextedit non-pointer:
    static QTextEdit textview --Lister.h
    And define its properties in such kind
    Lister::textview.setLineWrapMode(QTextEdit::NoWrap );
    But I cannot add textedit to layout as other ones are added as ponters:
    Qt Code:
    1. layout->addWidget(label3);
    2. layout->addWidget(Lister::textview); -- got not matching functions error
    3. //candidates are: void QBoxLayout::addWidget(QWidget*, int, Qt::Alignment).
    To copy to clipboard, switch view to plain text mode 
    I want to use static textedit in static-slot function so it could be applicable in threadwarapper function, as non-static results in error-carsh as I have written early.
    Despite the solution could be using non-static function with non-static texedit as well, putting it in static-warpper function of Lister class, then putting it in Thread::run, then calling Thread::start, and putting it in slot of threadwrapper function -- but it should be 4-tier hierarchy, but creation the object at first level could also lead to crash as in case of 3-tier option.
    If you dont understand about Qdialog so I can refer to some code:
    Qt Code:
    1. void Lister::walki() {
    2. QFileDialog* filedlg;
    3. QString path0=filedlg->getOpenFileName();//If I put these 2 lines in this method and then put this method in thread it lead to crash as without it would work fine if put to thread as without thread -- gui freeze..
    4. QDirIterator* it = new QDirIterator(path0, QDirIterator::Subdirectories);
    To copy to clipboard, switch view to plain text mode 
    Last edited by artt; 27th December 2015 at 23:03.

  4. #84
    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 try to define such textedit:
    Lister.h:
    static QTextEdit* textview;
    In Lister.cpp:
    //Lister::textview = new QTextEdit(); -- in this case I got yellow background. So error.
    What is the actual error message?

    Quote Originally Posted by artt View Post
    So I try to make qtextedit non-pointer:
    static QTextEdit textview --Lister.h
    And define its properties in such kind
    Lister::textview.setLineWrapMode(QTextEdit::NoWrap );
    But I cannot add textedit to layout as other ones are added as ponters:
    Qt Code:
    1. layout->addWidget(label3);
    2. layout->addWidget(Lister::textview); -- got not matching functions error
    3. //candidates are: void QBoxLayout::addWidget(QWidget*, int, Qt::Alignment).
    To copy to clipboard, switch view to plain text mode 
    You forgot the & to get the object's address.

    Quote Originally Posted by artt View Post
    I want to use static textedit in static-slot function so it could be applicable in threadwarapper function
    Since Lister is the class that you run in a thread, it would be better if it did not have some UI members.
    And using static methods as slots is very uncommon, better do it properly.

    Quote Originally Posted by artt View Post
    If you dont understand about Qdialog so I can refer to some code:
    Qt Code:
    1. void Lister::walki() {
    2. QFileDialog* filedlg;
    3. QString path0=filedlg->getOpenFileName();//If I put these 2 lines in this method and then put this method in thread it lead to crash as without it would work fine if put to thread as without thread -- gui freeze..
    4. QDirIterator* it = new QDirIterator(path0, QDirIterator::Subdirectories);
    To copy to clipboard, switch view to plain text mode 
    Since UI can be used from secondary threads the obvious way is to use the dialog in the slot that is connected to the button, get the file name, then start the thread with the obtained data.

    Oh, and you probably want to use getExistingDirectory().

    Cheers,
    _

  5. #85
    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 is the actual error message? -- Something like "undefined reference"
    "You forgot the & to get the object's address" - I tried to do
    layout->addWidget(&Lister::textview), layout->addWidget(Lister:&textview)) - but it is incorrect.
    "Since Lister is the class that you run in a thread, it would be better if it did not have some UI members" - how can I assign qtextedit or extract the qlinedit, and then put in SLOT() connect - it was early in main() but it is rather impossible to organize in such way.
    "And using static methods as slots is very uncommon, better do it properly" - but it exactly works well, maybe as non-static, but in threadwrapper exactly non-static lead to crash. So I assumed as indexate and writexmlfile is static maybe just them put in thread but renderdirectly and renderfromxml (with non-static) put in GUI thread as it it crash in thread for now, as well as the search function. So what is "properly" for you?
    I think that of course the dialogchoosers could be put in Main gui or in separate thread.
    Last edited by artt; 28th December 2015 at 21:28.

  6. #86
    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 is the actual error message? -- Something like "undefined reference"
    Maybe you forgot to define the variable and only have it declared?

    In general not providing the forum with the actual error message makes it impossible to determine what could be wrong and suggest fixes.

    Quote Originally Posted by artt View Post
    "Since Lister is the class that you run in a thread, it would be better if it did not have some UI members" - how can I assign qtextedit or extract the qlinedit, and then put in SLOT() connect - it was early in main() but it is rather impossible to organize in such way.
    Since you give no indication what you are actually trying to do we can only guess.
    Just make the connection from the place were you have both the lister object and the text edit.

    Quote Originally Posted by artt View Post
    "And using static methods as slots is very uncommon, better do it properly" - but it exactly works well, maybe as non-static, but in threadwrapper exactly non-static lead to crash. So I assumed as indexate and writexmlfile is static maybe just them put in thread but renderdirectly and renderfromxml (with non-static) put in GUI thread as it it crash in thread for now, as well as the search function. So what is "properly" for you?
    You can use static functions if you don't need them to access any data from the instance of the object.
    When you start to need static members then it is usually a good hint that these are not static methods at all.

    It is hard to keep track of what you do with which class, but it seems you are trying to do too much in one class.
    Lets assume Lister is the class that you work with on the higher level.
    It will have a slot to ask for the base directory and then create an instance of your walker thread.
    That thread class then has the method to traverse the directory tree and keeps the results in a member.
    The lister can then retrieve the result once the thread is done.

    If you need to report results as they come in, then let the thread emit them via signal instead and receive them in a slot of the Lister.

    No need for any static methods or static members, all operations in Lister happen in the main thread, can use UI there as well.

    Cheers,
    _

  7. #87
    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 defined textview (qtextedit) variable in Lister.cpp (in constructor) and declared in Lister.h. I got undefined reference when I put it Lister::textview, and make it static before. When not static, and textview* pointer it did not induce such error. So may be issue is in getting the adress/pointer of static variable? When not static I could not use it in static slot -- you should understand That I need to make Lister object in run() of thread then I could not refer to such object in connect() statement as there is another general Lister object so it is the reason to use static method in threadwrapper SLOTS...


    Added after 8 minutes:


    walk() as traverse method do not induce error so I can use it as static and in separate thread. The results of walking is stored in static qlist listed variable and it works enough fine as I can pass such list of properties as for the xmlcreation file as well for direct rendering of gotten xml to textview. But as I have said I could put file chooses dialog in walk() and then in thread as I got the error that dialog should be in main gui thread. So I need put it there or in separate thread as well as with separate button for file chooser.
    Last edited by artt; 30th December 2015 at 07:48.

  8. #88
    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 defined textview (qtextedit) variable in Lister.cpp (in constructor) and declared in Lister.h.
    You cannot define a static variable inside a constructor.
    A static variable is always defined with its type and fully qualified classname and, optionally, a default value.

    Quote Originally Posted by artt View Post
    I got undefined reference when I put it Lister::textview, and make it static before.
    Sounds like you have not variable definition.

    Quote Originally Posted by artt View Post
    When not static I could not use it in static slot -- you should understand That I need to make Lister object in run() of thread then I could not refer to such object in connect() statement as there is another general Lister object so it is the reason to use static method in threadwrapper SLOTS...
    Right, but if you consider the responsibilities, then you see that the thread does not need to call anything in Lister if Lister is the class that controls the overall process.

    Lister would simply get the user input, create a thread with that input and then run the thread.
    The thread class would travese the directory tree and either send results as it goes or just store it until it is done.
    Lister would then either listen to the updates or retrieve the final result.

    Quote Originally Posted by artt View Post
    walk() as traverse method do not induce error so I can use it as static and in separate thread.
    Since it is not used anywhere else, you can just put it into the thread.

    Quote Originally Posted by artt View Post
    The results of walking is stored in static qlist listed variable
    If you put the method into the class that needs it, you can simply store the data in a non-static member.

    Cheers,
    _

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

    "You cannot define a static variable inside a constructor.
    A static variable is always defined with its type and fully qualified classname and, optionally, a default value."
    --Yes I will see it later in my Qt creator, as the static Qlist <Filewalker> listed definition was the first issue on this Qt project
    --How it was simplier in java - I have just one class Filewalker, and it worked.

  10. #90
    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 same symbol rules apply in C++ as well, a class name can only be used once.

    You need a different package (Java) or namespace (C++) to use a class name again.

    Cheers,
    _

  11. #91
    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 what you mean under using class name again (need new namespace). But I defined static variable (widget) - but due to it I got such error (as it is static widget definition) --
    "QWidget: Must construct a QApplication before a QPaintDevice" -
    as I have read about it on this forum it is because the static variable (textview) appear before the application so as I
    understand before Lister (f) object with other widgets, and due to global feature of static widget it appears that it is "physically" disconnected with other widgets (that is indside window-qwidget). So probably it is unreal to use static widget in such case, and the advice to delete inheritance on Qwidget is unreal also. So just to make it non-static probably...
    Despite this issue is not the main just using non-static SLOT-function in threadwarapper SLOT, that needs the static slot inside run(), that in turn nessesiate the static variable(widget).
    So is it possibel to use static widget?
    Or not create Lister object inside run(), but call its non-static SLot-function?

  12. #92
    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 what you mean under using class name again (need new namespace).
    You wrote that in Java there would only be one FileWalker class. That is the same in C++, class names need to be unique.

    Quote Originally Posted by artt View Post
    But I defined static variable (widget) - but due to it I got such error (as it is static widget definition) --
    "QWidget: Must construct a QApplication before a QPaintDevice" -
    Good point, another very good reason not to do static objects.

    Quote Originally Posted by artt View Post
    Despite this issue is not the main just using non-static SLOT-function in threadwarapper SLOT, that needs the static slot inside run(), that in turn nessesiate the static variable(widget).
    There is no reason whatsoever to call a static method from inside run(),

    Quote Originally Posted by artt View Post
    Or not create Lister object inside run(), but call its non-static SLot-function?
    I have lost track of what Lister is, but you seem to try to shoe-horn to things into one class.

    You need
    - a class that the application works with, that provides a slot that asks for user input, creates a thread, runs the thread and collects the result
    - a thread that runs the directory traversal

    If you want you can of course split the thread in two classes, the thread itself and a class that implements the directory traversal.

    There is no static required in any of this.

    Cheers,
    _

  13. #93
    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)

    ASSERT failure in QWidget: "Widgets must be created in the GUI thread." - here I got such error when placing non-static method in threadwrapper SLOT - it seems the same as with Qdialog (that I can find solution or absolve using of it) - it is due exactly to non-static method that uses non-static Qtextedit textview. That is why if it is also insolubilitable issue - I can set back using such non-static method is threads -that probably is affordable here. I use "textview" in readfromxmlfile and renderingdirectly xml to Qxtextedit, so it should not be very time consuming work (in Qt it seems to be faster then in Java, despite I did not tried it for the whole disk), and clearing textedit - so I could leave it in Gui-thread, as well the filenamesearch method that exploit another Qwidget(Qlabel) for the rendering finded result so I laso could not allow to use it as static.
    Anyway I will refer here for the whole code so the solution could be proposed - f.e. with signals, despite the signals are buttons in my case.

  14. #94
    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
    ASSERT failure in QWidget: "Widgets must be created in the GUI thread." - here I got such error when placing non-static method in threadwrapper SLOT - it seems the same as with Qdialog (that I can find solution or absolve using of it) - it is due exactly to non-static method that uses non-static Qtextedit textview.
    As the error clearly indicates you are trying to create a widget from a thread that is not the main thread.
    Whether that code is in a static or non-static method does not matter a bit.
    This has obviously nothing to do with static vs non-static.

    Cheers,
    _

  15. #95
    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, this error appears exactly here where I use non-static method in qthread::run(), as the static methods such as walk() and writexmlfile() that do not use Qwidgets works as in thread as without it - but walk() in GUI-thread freeze extremely, so thread is nessesary there anyway. If turn back to my first topic on forum about Swingworker analogue in Qt, that in Java swingworker is used for background processes such as traversing, but probably the assigning the text (despite very large) to Qtextedit is not so one, so the issue could be solved in manner not using threads for rendeing..
    here is whole code:http://s000.tinyupload.com/?file_id=...85004786237054

  16. #96
    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
    No, this error appears exactly here where I use non-static method in qthread::run()
    No sorry.
    It happens because it is called from QThread::run(), not because the method is non-static.


    Quote Originally Posted by artt View Post
    that in Java swingworker is used for background processes such as traversing
    And the same would work in C++ as well.

    Quote Originally Posted by artt View Post
    but probably the assigning the text (despite very large) to Qtextedit is not so one
    This has been explained before: UI can only be interacted with from the main thread.
    You worker thread either emits data as it goes and the a in the main thread handles it, or it just runs and you handle the result once it has completed its task.

    - Lister is your main class, it handles the user interaction
    - don't create instances of Lister in threads
    * it is a widget
    * it deals with the user interaction
    * you already have an instance of Lister
    - don't call any methods of Lister in the threads
    - put code that you want the threads to execute into the thread classes

    Cheers,
    _

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

    1. It happens because it is called from QThread::run(), not because the method is non-static
    2. - don't create instances of Lister in threads
    These are two fine remarks. but - how would you call (1) non-static method in run, without (2) creating
    Lister object - the [static] Lister::walk() works from within run(), because I do not use non-statci method, I do not create Lister there, so I do not create no Qwidget out of GUI-widget.
    3. Indeed I can left as it is - with 2 first methods in threads and others - out of there.
    4. About nessecity to create qwidgets just in Main-gui (despite I do not read about it exactly in official Qt documentation) I did understand, as It seems to be 2 different widgets, so not composite one, despite Java suppoer so. As it is impossible in Qt it is very big deficiency, as I also could not assign value to Qlabel, or take the text from Qlinedit - in separate thread.

  18. #98
    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
    but - how would you call (1) non-static method in run, without (2) creating
    Lister object - the [static] Lister::walk() works from within run(), because I do not use non-statci method, I do not create Lister there, so I do not create no Qwidget out of GUI-widget.
    See the last two points.

    Quote Originally Posted by artt View Post
    I also could not assign value to Qlabel, or take the text from Qlinedit - in separate thread.
    Trivial to do with a signal from the thread connected to a slot in the label, line edit or any other class on the main thread.

    Cheers,
    _

  19. #99
    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)

    "Trivial to do with a signal from the thread connected to a slot in the label" -- it is not so trivial for me.
    I have looked for signals yesterday, and saw such sample
    ...Thread.h
    signals: void signalmethod();
    public: run();
    then in thread.cpp
    Thread::run() {
    signalmethod();
    } but I did it with static method. But here is non-static one?? what kind (how it should look like) of signal I shpuld use?
    2. what 2 last point?

  20. #100
    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 have looked for signals yesterday, and saw such sample
    Right, just declare a signal and emit it in run.
    The code creating the thread object can then connec this signal to its own slot or to the slot of another class.

    E.g. if you had a signal like this

    Qt Code:
    1. signals:
    2. void updateText(const QString &text);
    To copy to clipboard, switch view to plain text mode 
    one could directly connect it to a label that should show that text
    Qt Code:
    1. connect(thread, SIGNAL(updateText(QString)), label, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by artt View Post
    2. what 2 last point?
    Quote Originally Posted by anda_skoa View Post
    - don't call any methods of Lister in the threads
    - put code that you want the threads to execute into the thread classes

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.