Page 1 of 8 123 ... LastLast
Results 1 to 20 of 154

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

  1. #1
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

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

    I need to realize several buttons (as well as Qlabels, QtextEdit) in QWidget.
    But I need to do it non-lockable, so if it is executinh a long time, it should be not hanging.
    In java it works very fine with SwingWorker class, but not with Runnable.
    So I need to know what analog I need to use in Qt.
    Here is excerpts from java code. As it is very compact so I need to follow it in Qt but with local
    classes:
    Qt Code:
    1. class Writefile extends SwingWorker {//Declaration of custom thread class
    2. public Object doInBackground() {
    3. XMLCreator.stax();//class and method that realize XML writing to file.
    4. return null;
    5. }
    6.  
    7. writetofile.addActionListener(new ActionListener() { //adding listener to button
    8. public void actionPerformed(ActionEvent e) {
    9. //XMLCreator.stax();
    10. Writefile process = new Writefile(); //initizalization of swingworjer class
    11. process.execute(); //starting separate task in separate thread
    12. }
    13. });
    To copy to clipboard, switch view to plain text mode 
    So how should I use
    Qt Code:
    1. Qtobject::connect(writebutton,SIGNAL(clicked()), this, SLOT(Filewalker::writetofile()));
    To copy to clipboard, switch view to plain text mode 
    //The last is the slot declared in Filewalker class header
    Last edited by artt; 13th December 2015 at 19:08.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    All QWidget instances must be created and run in the main GUI thread.

    What you might be able to do is to send a signal to a different thread upon a push-button click. That is, the pushbutton click is handled in a slot in the main thread, but it sends a signal to a non-GUI handler slot in a different thread.

    Google for Wysota's excellent article on "Keeping the GUI responsive".

  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)

    There is an part about workerthread: Qthread. So I do understand that I need to extend the Qthread, put the
    Filewalker::writetofile() in run() method then just initialize Qthread2 in Qwidget in such way as
    Qthread2 writes; And then put it in connect method in slot like SLOT(writes.exec()).
    Qt Code:
    1. Qtobject::connect(writebutton,SIGNAL(clicked()), this, SLOT(Filewalker::writetofile()));
    2. Qtobject::connect(writebutton,SIGNAL(clicked()), &writes, SLOT(writes.exec());
    To copy to clipboard, switch view to plain text mode 
    If so should I use "this" in the connect 3-rd argument, or put the instance of Qthread2?
    //Despite in QAssistant the start() is prevalent, as I see, to exec().
    //There is also the question about how and where (in what file) to declare and initialize several Qthreads?
    In Java - I do it in Jframe file, could I do it in Qwidget file?

  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)

    Despite in QAssistant the start() is prevalent, as I see, to exec().
    QThread::start() starts the thread, i.e. requests a thread from the operating system and lets it execute run().

    QThread::exec() can be called from within run() to start the thread's event loop. The default implementation of QThread::run() basically does just that.

    If so should I use "this" in the connect 3-rd argument, or put the instance of Qthread2?
    The third argument of connect() is the receiver object, so it depends which object you want to execute the slot on.

    The equivalent to your Java code would be to connect to "this" and in that slot create the worker thread and start it.

    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)

    I though that I should do in such way(I didnot explored Qthread enough yet):
    Qt Code:
    1. class Writefile: public QThread {
    2. public void run() {
    3. Filewalker::writetoXML();
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 
    here is question is it enough just put Filewalker.h (just with Filewalker::writetoXML() declaration, but definition in main.cpp --really seems improbable, so I think I need to create header for writetoXML() --writetoXML.h ) in aforementioned file with Writefile
    then, to initialize Qthread in QWidget:
    Qt Code:
    1. Writefile wr; //(or Writefile* wr=new WriteFile();)
    2. Qtobject::connect(writebutton,SIGNAL(clicked()), this (//or &wr), SLOT(wr.exec() //or wr.start);
    To copy to clipboard, switch view to plain text mode 
    What is incorrect, it is my blueprint...

    So should I Extend MyWidget the Qobject or can I realize just with main(widget).cpp --that is--should I declare the slots in final Widget file or in initial file such as FileWalker.h
    or methodheader - writetoXML.h?

    So maybe I should abstain of this console file from here http://www.qtcentre.org/threads/6455...197#post285197 and put all definition of
    slots (that are functions from Filewalker) in one big Widget File that defines such functions as (indexate files, writetoxml, renderxml, searchbyname) with QButtons, Qlabels, and connect statements.
    The question is appered in such case -- where to define Qthread class for every button (in header?), and then initialize it in Widget file???
    Last edited by artt; 14th December 2015 at 00:47.

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

    here is question is it enough just put Filewalker.h (just with Filewalker::writetoXML() declaration, but definition in main.cpp
    Yes

    What is incorrect, it is my blueprint...
    The SLOT macro needs a slot signature. That is the slot's name and, if applicable, all argument types.

    should I declare the slots in final Widget file
    Well, that is a non-question, the slot needs to be a member of the receiver class, which is already declared somewhere.
    So if "this" is your receiver object, then the slot will be part of the declaration of that class.

    where to define Qthread class for every button (in header?), and then initialize it in Widget file???
    If you want to keep references to the thread(s) then yes, as a member of the class that starts them would be a good choice.

    Cheers,
    _

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Why bother with QThreads? Just use QtConcurrent::run(). Here:
    Qt Code:
    1. ...
    2. connect(myButton, SIGNAL(clicked()), SLOT(runMyFunctionInAnotherThread()));
    3. ...
    4. void myFunction() {
    5. // This function is not meant to be run in the GUI thread
    6. }
    7. ...
    8. void MyClass::runMyFunctionInAnotherThread() {
    9. QtConcurrent::run(myFunction);
    10. }
    To copy to clipboard, switch view to plain text mode 

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

    QtConcurrent::run() might be a bit weird to use when the function generates a result (usage of the QFuture API).

    One also needs to be aware that the number of threads is limited, especially when using the overload that does not take a thread pool but uses the global instance.

    And at least in one occasion I had a task not run in a separate thread. Not sure if that was a bug or something like work-stealing in case of an overloaded thread pool, I don't trust it to be run separately.

    Cheers,
    _

  9. #9
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    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 anda_skoa View Post
    QtConcurrent::run() might be a bit weird to use when the function generates a result (usage of the QFuture API).
    I concede that other QtConcurrent operations have a complicated way of returning results, but QtConcurrent::run() is actually fairly simple. Just set up a QFutureWatcher. When it emits finished(), you can immediately recover the result by calling result(). This is as simple as an asynchronous API can be (well, maybe a signal finished(T result) would be even simpler). A QThread-based solution will end up with a similar interface.

    Quote Originally Posted by anda_skoa View Post
    One also needs to be aware that the number of threads is limited, especially when using the overload that does not take a thread pool but uses the global instance.
    Sure, but unless fine-grained control over the threads is required (which does not seem to be a primary concern of the OP), it makes sense to let QtConcurrent adapt the number of concurrent jobs to the actual capacity of the system.

    Quote Originally Posted by anda_skoa View Post
    And at least in one occasion I had a task not run in a separate thread. Not sure if that was a bug or something like work-stealing in case of an overloaded thread pool, I don't trust it to be run separately.
    This probably is a bug; the documentation specifically mentions that the function is run in a separate thread.

    In summary, this looks to me like the standard use case for QtConcurrent::run(): the OP just needs a high-level, concise API to run a task in a separate thread, and trusts the platform with the details of job management and scheduling.

  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 think maybe it is better to convert my console(main.cpp) file with function definition in widget(main.cpp), or just simply copy from one to another as thelast require additional form file - as it is inevident that definition in another cpp would be workable
    in main.cpp (how can i include another cpp to main.cpp, as I do it with filewalker.h--if I include filewalker.h with function declaration - do another cpp know where to find its definition or is it present in the whole?).
    About this pattern of code that should be present probably in Widget file:
    Qt Code:
    1. connect(myButton, SIGNAL(clicked()), SLOT(runMyFunctionInAnotherThread()));
    2. ...
    3. void myFunction() {
    4. // This function is not meant to be run in the GUI thread
    5. }
    6. ...
    7. void MyClass::runMyFunctionInAnotherThread() {
    8. QtConcurrent::run(myFunction);
    9. }
    To copy to clipboard, switch view to plain text mode 
    As I understood -- MyClass shoud some kind of Qthread(if I use this threadclass) and its MyClass.h should be included in Widgetfile.
    "myFunction()" -- should be some kind as Filewalker::writexmltofile() definition?
    And void MyClass::runMyFunctionInAnotherThread() should use MyClass::startthread() when run() found in MyClass declaration.
    Where is SLOT signature here? Or it should not be declared as slot in the same file where is connect() statement? Where is 3-rd - receiving object - argument here? "this" or so?
    Last edited by artt; 14th December 2015 at 17:31.

  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)

    Quote Originally Posted by yeye_olive View Post
    I concede that other QtConcurrent operations have a complicated way of returning results, but QtConcurrent::run() is actually fairly simple. Just set up a QFutureWatcher. When it emits finished(), you can immediately recover the result by calling result(). This is as simple as an asynchronous API can be (well, maybe a signal finished(T result) would be even simpler). A QThread-based solution will end up with a similar interface.
    I generally agree, but you have to know that there is QFutureWatcher and the API is a bit uncommon, even for experienced C++ developers.
    But since the OP even has difficulties with basic C++ such as heap allocation or header/source file splits, I have my doubts the QtConcurrent::run() would be understandable.

    Quote Originally Posted by yeye_olive View Post
    Sure, but unless fine-grained control over the threads is required (which does not seem to be a primary concern of the OP), it makes sense to let QtConcurrent adapt the number of concurrent jobs to the actual capacity of the system.
    Well, thread pool based tasks should be non-blocking (CPU bound), not sure if that is the case of the tasks the OP wants to run.

    Quote Originally Posted by yeye_olive View Post
    This probably is a bug; the documentation specifically mentions that the function is run in a separate thread.
    Yes, probably, or different in Qt4 which I used at that time.

    Cheers,
    _

  12. #12
    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 tried to do more simply: I created widget project and launched such code in main method and renamed main.cpp(in filewalker) to Filewalker.cpp. And after launching such code --
    Qt Code:
    1. #include <QApplication>
    2. #include <QVBoxLayout>
    3. #include <QSlider>
    4. #include <QSpinBox>
    5. #include <QPushButton>
    6. #include <QLabel>
    7. #include <QFileInfo>
    8. #include "Filewalker.h"
    9. int main(int argc, char *argv[])
    10. {
    11. Filewalker f;// no matching function for call to 'Filewalker::Filewalker()' -- is there no default constructor for it?
    12. //Just candidates are: Filewalker::Filewalker(long int, QString, QString); and Filewalker::Filewalker(const Filewalker&)
    13. QApplication app(argc, argv);
    14. QWidget window;
    15. //QWidget *window = new QWidget;
    16. window.setWindowTitle("File Indexer");
    17. QPushButton *indexate = new QPushButton("&Indexate Files");
    18. QPushButton *writexml = new QPushButton("&WriteXMLtoFile");
    19. QPushButton *readxml = new QPushButton("&ReadXMLfromFile");
    20. QPushButton *render = new QPushButton("&RenderDirectly");
    21. QLabel *label1 = new QLabel();
    22. QLabel *label2 = new QLabel();
    23. QPushButton *search = new QPushButton("&Search file by name");
    24. QObject::connect(indexate, SIGNAL(clicked()),&f, SLOT(Filewalker::walk("C:\\C"))); //no matching function for call to //'QObject::connect(QPushButton*&, const char*, Filewalker&, const char*)' -- what const char* means especially in the last //case, in the first one -- clicked() seams to be normal signal for SIGNAL. How should I describe slot here. Or it is the result of Q_Object macro despite the error seems to be another in such case.
    25. QVBoxLayout *layout = new QVBoxLayout();
    26. layout->addWidget(indexate);
    27. layout->addWidget(writexml);
    28. layout->addWidget(readxml);
    29. layout->addWidget(render);
    30. layout->addWidget(label1);
    31. layout->addWidget(label2);
    32. layout->addWidget(search);
    33. window.setLayout(layout);
    34. window.show();
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    In the first one I should use not Filealker but Lister class for slot functions such as indexate, writetoxml, so filewalker should be just for objects. And Lister should have default constructor as I initialized it - but without no declared constructor.
    If I really need to inherite QObject and include Q_OBJECT macro should I rename such main.cpp in widget..cpp (and delete main() declaration and its brackets) what should I include in another main.cpp method --as In molketin there is just sample of dialog.h , dialog.cpp --ot I should obligatorty include button, windows, labels and connect in main() method??
    Last edited by artt; 15th December 2015 at 02:32.

  13. #13
    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
    Filewalker f;// no matching function for call to 'Filewalker::Filewalker()' -- is there no default constructor for it?
    //Just candidates are: Filewalker::Filewalker(long int, QString, QString); and Filewalker::Filewalker(const Filewalker&)
    If the compiler says there is no argument-less constructor then it is very likely right.
    Check your class declaration.

    Quote Originally Posted by artt View Post
    QObject::connect(indexate, SIGNAL(clicked()),&f, SLOT(Filewalker::walk("C:\\C"))); //no matching function for call to //'QObject::connect(QPushButton*&, const char*, Filewalker&, const char*)'
    Is "f" an object of a class derived from QObject. Does it have the Q_OBJECT macro.
    Also SLOT(), like SIGNAL(), takes the name of a function and its arguments. Since the signal does not have any arguments that it could pass on, the slot has no arguments either.
    So a valid SLOT() content could look like this:
    Qt Code:
    1. QObject::connect(indexate, SIGNAL(clicked()),&f, SLOT(someSlot()));
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by artt View Post
    -- what const char* means especially in the last //case, in the first one -- clicked() seams to be normal signal for SIGNAL.
    SIGNAL() and SLOT() are macros that convert their arguments into const char*, the type a string literal has in C and C++.

    Quote Originally Posted by artt View Post
    In the first one I should use not Filealker but Lister class for slot functions such as indexate, writetoxml, so filewalker should be just for objects. And Lister should have default constructor as I initialized it - but without no declared constructor.
    I am afraid I don't know what you mean with the first part, but yes, a class without any declared constructor has a default constructor.

    Quote Originally Posted by artt View Post
    If I really need to inherite QObject and include Q_OBJECT macro should I rename such main.cpp in widget..cpp (and delete main() declaration and its brackets) what should I include in another main.cpp method --as In molketin there is just sample of dialog.h , dialog.cpp --ot I should obligatorty include button, windows, labels and connect in main() method??
    Again not sure if I interpret this correctly, but yes, it makes sense to create a widget class that contains the UI building and then just instantiate it in main().

    Cheers,
    _

  14. #14
    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 do "someslot" should look like? I defined the Filewalker::walk() (as a function for indexation) as public slot in Filewalker.h - is it not enough?? And I think that all slots like walk(), writetoxmlfile() and qlist <filewalker*> should be put in Lister as class with default constructor (is it possible in such way to use class Lister: public QObject) and Filewalker as class just with fields. So I would put &f as the reference to Lister object. Then how to use UI Widget class just to put the code from current main () to some kind as class guis: public qwidget? Should I do some modification of automatic shaped .ui form file?

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    Artt - I realize that you may be a skilled Java programmer, but it is obvious from your questions that you do not know much about either C++ or Qt. You are trying to run before you even know how to crawl.

    Qt comes with many, many C++ example programs to help you learn all aspects of Qt. Many of the questions you are asking could be answered if you studied the examples and learned from them. Right now, you are asking so many questions which are so confused that it is almost impossible to know where to start to answer them.

    So open Qt Creator, click on the Welcome page and look at the Examples or the "Getting Started Programming with Qt Widgets" Tutorial. I think you will find it more helpful than trying to learn C++ and Qt programming from us.

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

    Yes it is my first programm in Qt, despite in Java I have just several ones, but is more competent in it and this language is more light to learn indeed.
    I have read somewhere, that you should learn C++, but in Qt just read the documentation.
    So here I want to try to do relative complex widget, in one thread, then if succesful then I will do it in multi-thread.
    So what I would not read in documentation and examples-- can I absolve of .ui file, can it be blank, should i make some modification of it due the specific of my current task. Then could I use in GUI Qt project just one file for example Filewalker.cpp and call its widget realization in its main()?? Why to do another file for just main()?
    //And I do not know why when I opened last yesterday project in Qtcreator - I see grey(disabled) menu of Qtcreator - and I could not activate it anyway?


    Added after 36 minutes:


    If I modified the Lister.h file by subclassing QObject and transferring slots from Filewalker.h in such way:
    Qt Code:
    1. #ifndef LISTER_H
    2. #define LISTER_H
    3. #include <QObject>
    4. #include "Filewalker.h"
    5. class Lister : public QObject
    6. {
    7.  
    8. Q_OBJECT
    9. public:
    10. public: Lister (QObject* parent = 0): QObject(parent){} //By adding such constructor errors not disappear --
    11. static QList <Filewalker*> listed;
    12. //static QTextStream cout(FILE * fileHandle);
    13. public slots:
    14. static void walk(QString path0);
    15. static void writexmlfile(QString path1);
    16. static void readxmlfile();
    17. static void searchname();
    18. };
    19. #endif
    To copy to clipboard, switch view to plain text mode 
    I got such error in "upper" main() (when also there substituting Filewalker for Lister) --
    In function `Lister':
    undefined reference to `vtable
    In function `~Lister':
    undefined reference to `vtable
    //As looking at this pattern (one of the first in Google) http://www.java2s.com/Code/Cpp/Qt/extendsQWidget.htm
    it is very close to mine:
    I have also Lister.h, FileWalker.cpp (can call also Lister.cpp) with slots definition, and main() with connect statement so --the
    issue know is in constructor and destructor in my Lister.h definition, should it subclass Qobject or QWidget, and how to
    describe correctly constructor and destructor for it to free of this 2 same errors?
    So here I did not have even no example -- as I have it--despite the errors are present.
    //And why such code the same in structure of extending QWidget do not work in my case, despite in
    http://www.java2s.com/Code/Cpp/Qt/extendsQWidget.htm - probably works
    Qt Code:
    1. #ifndef LISTER_H
    2. #define LISTER_H
    3. #include <QWidget>
    4. #include "Filewalker.h"
    5. class Lister : public QWidget
    6. {
    7.  
    8. Q_OBJECT
    9. public: Lister (QWidget *parent = 0 );
    10. static QList <Filewalker*> listed;
    11. //static QTextStream cout(FILE * fileHandle);
    12. public slots:
    13. static void walk(QString path0);
    14. static void writexmlfile(QString path1);
    15. static void readxmlfile();
    16. static void searchname();
    17. };
    18. #endif
    To copy to clipboard, switch view to plain text mode 
    After initializing the Lister in main() in such way:
    Qt Code:
    1. QApplication app(argc, argv);
    2. QWidget window;
    3. //QWidget *window = new QWidget;
    4. window.setWindowTitle("File Indexer");
    5. Lister f(QWidget *window = 0);
    To copy to clipboard, switch view to plain text mode 
    I got error about connect:
    no matching function for call to 'QObject::connect(QPushButton*&, const char*, Lister (*)(QWidget*), const char*)'
    candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)..etc -- so I understand that I need correct placing of receiving object despite it is like as in examples from java2s, or to eradication of vtable error...
    Last edited by artt; 16th December 2015 at 01:15.

  17. #17
    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 do "someslot" should look like? I defined the Filewalker::walk() (as a function for indexation) as public slot in Filewalker.h - is it not enough??
    Yes, that is enough.

    Quote Originally Posted by artt View Post
    And I think that all slots like walk(), writetoxmlfile() and qlist <filewalker*> should be put in Lister
    That would be ok as well.

    Quote Originally Posted by artt View Post
    as class with default constructor
    Type of constructor doesn't matter at all, no difference to Java.

    Quote Originally Posted by artt View Post
    (is it possible in such way to use class Lister: public QObject) and Filewalker as class just with fields. So I would put &f as the reference to Lister object.
    Sure, why not.

    Quote Originally Posted by artt View Post
    undefined reference to `vtable
    Make sure that the header for Lister is in the HEADERS variable in your .pro file and re-run qmake.

    Cheers,
    _

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

    Untill now the 2 headers were absent as there is 2 examples of new Lister=Filewalker combination (nested meanwhile so last one jhave no such headers as Filewalkers.cpp with definitions of slots). But after updating it and quiting-opening Qtcraetor -- the Build-Debug menu is inactive again - so I cannot check with new conditions.
    Here I provid my current pro file:
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = Filewalker2
    6. TEMPLATE = app
    7.  
    8.  
    9. SOURCES += main.cpp Filewalker.cpp filewalker2.cpp
    10.  
    11. HEADERS += filewalker2.h Lister.h Filewalker.h
    12.  
    13.  
    14. FORMS += filewalker2.ui
    To copy to clipboard, switch view to plain text mode 
    The filewalker2.h and filewalker2.cpp were created during the creation of project so could I
    delete them, how they influence the my native files? Maybe there is some bad interaction?

    #include "filewalker2.h"
    #include "ui_filewalker2.h"

    Filewalker2::Filewalker2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Filewalker2)
    {
    ui->setupUi(this);
    }

    Filewalker2::~Filewalker2()
    {
    delete ui;
    }

    and header:
    #ifndef FILEWALKER2_H
    #define FILEWALKER2_H

    #include <QWidget>

    namespace Ui {
    class Filewalker2;
    }

    class Filewalker2 : public QWidget
    {
    Q_OBJECT

    public:
    explicit Filewalker2(QWidget *parent = 0);
    ~Filewalker2();

    private:
    Ui::Filewalker2 *ui;
    };

    #endif // FILEWALKER2_H

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

    If you don't need these files, then of course you can delete them if you also remove their name occurences from the .pro file.

    Cheers,
    _

  20. #20
    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 why build-debug menu is inactive? Why I should close and open Qtcreator - and it doesnt help. Yesterday -- very different manipulation lead to some results, but why to absolve this problem in once?

    Can I also delete filewalker2.ui form file?

Similar Threads

  1. Replies: 1
    Last Post: 1st April 2014, 09:48
  2. Destruction in separate threads
    By KevinKnowles in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2012, 10:49
  3. Problem with QProcess in two separate threads
    By viheaho in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 23:52
  4. Replies: 1
    Last Post: 7th December 2009, 08:26
  5. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 09: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.