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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

  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 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?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

  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)

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

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

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

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

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

  11. #11
    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?

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    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
  •  
Qt is a trademark of The Qt Company.