Results 1 to 17 of 17

Thread: Call function of object within Threads.

  1. #1
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Call function of object within Threads.

    Hello everyone.

    I'm studying about threads and would like some help.
    Learn how to perform functions that are on an object that has been moved to a thread.
    I have to pass parameters to the function and receive the return of it to be able to display in a text edit.

    Oops: Forgetting that objects and threads are generated by a "FOR".

    first......: How to pass parameters to the function ???
    second.: How to receive the return of it to be able to display in a text edit ???

    I tried this:

    Qt Code:
    1. void MainWindow::on_cmdIniciar_clicked()
    2. {
    3. // ARRAY DE THREADS
    4. QThread *vthrThread[vnumCont];
    5.  
    6. // ARRAY DE OBJETOS
    7. clsTask *vobjTask[vnumCont];
    8.  
    9.  
    10. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    11. {
    12. vobjTask[vnumIni] = new clsTask();
    13. vthrThread[vnumIni] = new QThread();
    14.  
    15. //ERRO HERE
    16. //QObject::connect(&vthrThread[vnumIni], SIGNAL(started), vobjTask[vnumIni], SLOT(fcMensagemTexto(QString::number(vnumIni))));
    17.  
    18. vobjTask[vnumIni]->moveToThread(vthrThread[vnumIni]);
    19.  
    20. vthrThread[vnumIni]->start();
    21.  
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QObject::connect(&vthrThread[vnumIni], &QThread::started, &vobjTask[vnumIni], &clsTask::fcMensagemTexto(QString::number(vnumIni) ));
    To copy to clipboard, switch view to plain text mode 

    ERRO:
    cannot call member function 'QString clsTask::fcMensagemTexto(QString)' without object
    QObject::connect(&vthrThread[vnumIni], &QThread::started, &vobjTask[vnumIni], &clsTask::fcMensagemTexto(QString::number(vnumI ni) ));

    COMPILER OUTPUT
    error: cannot call member function 'QString clsTask::fcMensagemTexto(QString)' without object
    QObject::connect(&vthrThread[vnumIni], &QThread::started, &vobjTask[vnumIni], &clsTask::fcMensagemTexto(QString::number(vnumI ni) ));
    Last edited by marcos.miranda; 12th January 2017 at 21:00.

  2. #2
    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: Call function of object within Threads.

    If the thread has a single function and only executes it once, just pass the arguments to the class constructor.

    You can also use QMetaObject::invokeMethod() with connection type Qt::QueuedConnection to invoke a slot on the worker object in the worker thread's context.

    For the result you either need a signal on the worker object or use the invokeMethod() option the other way around on an object that belongs to the main thread.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    marcos.miranda (13th January 2017)

  4. #3
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Wink Re: Call function of object within Threads.

    Hi, anda_skoa.
    First of all, thank you for answering me.

    Before reading your considerations, I was modifying the project to simplify it and thus better understand how to work with threads and why it is giving error.

    You could take a look to see where I'm going wrong, the project was like this.

    Thank you for your attention.

    Qt Code:
    1. // Arquivo .h
    2. #include <QObject>
    3. #include <QThread>
    4. class clsThrTask : public QThread
    5. {
    6. Q_OBJECT
    7. public:
    8. clsThrTask(QString pstrIdThread, QObject *parent = nullptr); // Constructor with paramenter
    9. void run() override;
    10. signals:
    11. void sigRetorno(QString);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Arquivo .cpp
    2. #include "clsthrtask.h"
    3. clsThrTask::clsThrTask(QString pstrIdThread, QObject *parent) : QThread(parent)
    4. {
    5. vstrForRef = pstrIdThread;
    6. }
    7. void clsThrTask::run()
    8. {
    9. QString vstrResposta = "Threads nº: " + QString::number((long long) QThread::currentThreadId(),16) + " | FOR REF nº: " + vstrForRef;
    10. // EMITE UM SIGNAL WITH DATA
    11. emit sigRetorno(vstrResposta);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Arquivo MainWindow.h
    2. #include "clsthrtask.h"
    3. namespace Ui {
    4. class MainWindow;
    5. }
    6. class clsThrTask;
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit MainWindow(QWidget *parent = nullptr);
    12. ~MainWindow();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Arquivo MainWindow.cpp
    2. MainWindow::MainWindow(QWidget *parent) :
    3. QMainWindow(parent),
    4. ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7. }
    8.  
    9. // ARRAY OF THREADS
    10. QThread *vthrThread[vnumCont];
    11.  
    12. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    13. {
    14. vthrThread[vnumIni] = new QThread(QString::number(vnumIni)); // Erro here.
    15. QObject::connect(vthrThread[vnumIni], &clsThrTask::sigRetorno, ui->txtResultado, &QTextEdit::append); // Erro here.
    16. vthrThread[vnumIni]->start();
    17. }
    18.  
    19. 1) error: no matching function for call to 'QThread::QThread(QString&)' vthrThread[vnumIni] = new QThread(vstrIdThread);
    20.  
    21. 2) error: invalid conversion from 'QThread*' to 'const Object* {aka const clsThrTask*}' [-fpermissive]
    22. QObject::connect(vthrThread[vnumIni], &clsThrTask::sigRetorno, ui->txtResultado, &QTextEdit::append);
    23. ^
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Wink Re: Call function of object within Threads.

    Hi everyone

    I found out where I was wrong, I'm talking about the first mistake.

    VthrThread [vnumIni] = new QThread (vstrIdFor, this); Erro here.

    VthrThread [vnumIni] = new clsMultiThr (vstrIdFor, this); Correct here.

    Can someone help me in the second error.
    Someone ???

    2) Second erro is :
    Qt Code:
    1. QObject::connect(vthrThread[vnumIni], &clsMultiThr::sigRetorno, ui->txtResposta, &QTextEdit::append);
    2.  
    3. error: invalid conversion from 'QThread*' to 'const Object* {aka const clsMultiThr*}' [-fpermissive]
    4. QObject::connect(vthrThread[vnumIni], &clsMultiThr::sigRetorno, ui->txtResposta, &QTextEdit::append);
    5. ^
    To copy to clipboard, switch view to plain text mode 

    How would this connection work?


    The neurons are dying and the blood is already in the shins.

    Thank you for your attention.
    Last edited by marcos.miranda; 13th January 2017 at 15:50.

  6. #5
    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: Call function of object within Threads.

    Your array needs to be of the subtype as well.

    In general you might want to prefer a container, e.g. a vector, over a C array.

    Cheers,
    _

  7. #6
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Talking Re: Call function of object within Threads.

    Hi, Anda Skoa.

    I will have to convert an array of threads to an array of objects
    I'm not undestend, there is how to you show this in code for me to understand.

    Thanks..

  8. #7
    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: Call function of object within Threads.

    This:

    Qt Code:
    1. // ARRAY OF THREADS
    2. QThread *vthrThread[vnumCont];
    3.  
    4. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    5. {
    6. vthrThread[vnumIni] = new QThread(QString::number(vnumIni)); // Erro here.
    7. QObject::connect(vthrThread[vnumIni], &clsThrTask::sigRetorno, ui->txtResultado, &QTextEdit::append); // Erro here.
    8. vthrThread[vnumIni]->start();
    9. }
    To copy to clipboard, switch view to plain text mode 

    needs to be this:

    Qt Code:
    1. // ARRAY OF THREADS
    2. clsThrTask *vthrThread[vnumCont];
    3.  
    4. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    5. {
    6. vthrThread[vnumIni] = new clsThrTask(QString::number(vnumIni));
    7. QObject::connect(vthrThread[vnumIni], &clsThrTask::sigRetorno, ui->txtResultado, &QTextEdit::append);
    8. vthrThread[vnumIni]->start();
    9. }
    To copy to clipboard, switch view to plain text mode 

    As anda_skoa said, your array of threads need to be of the same type as your derived class. The compiler is complaining because QThread does not have a signal named "sigRetorno".

    Note that QTextEdit::append() is probably not an atomic method (that is, appending text is not protected by a mutex), so you could have two or more threads trying to modify the text edit at the same time. This could lead to memory corruption, a crash, or you might get lucky and it works just fine sometimes.

    You would be better off connecting to a slot in your own class that protects the append() method from simultaneous access using a mutex.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    marcos.miranda (15th January 2017)

  10. #8
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Talking Re: Call function of object within Threads.

    Hi, d_stranz's.
    I understood what you said, and it showed in code.
    I tested it and it worked now.

    Thank you very much for your explanation about the problem.
    I will be making the project available on the site for other QT beginners,
    To understand better about Threads.

    Just to finalize this case study, I noticed that through Applications Output the following error message.

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. (Parent is QProcess(0x239e2e0), parent's thread is QThread(0x1e16850), current thread is clsThrTask(0x239e300)
    3. QObject: Cannot create children for a parent that is in a different thread.
    4. (Parent is QProcess(0x239e2e0), parent's thread is QThread(0x1e16850), current thread is clsThrTask(0x239e300)
    5. QObject: Cannot create children for a parent that is in a different thread.
    6. (Parent is QProcess(0x239e2e0), parent's thread is QThread(0x1e16850), current thread is clsThrTask(0x239e300)
    To copy to clipboard, switch view to plain text mode 

    How do I resolve this?
    Could you tell me what I should change in the code.

    Thanks.
    Attached Files Attached Files

  11. #9
    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: Call function of object within Threads.

    Quote Originally Posted by d_stranz View Post
    Note that QTextEdit::append() is probably not an atomic method (that is, appending text is not protected by a mutex), so you could have two or more threads trying to modify the text edit at the same time.
    No problem in this case, as the signal/slot connection between two threads has the slot called in the context of the receiver object's thread.
    So append() is always called by the same thread, in this case the main thread.

    Quote Originally Posted by marcos.miranda View Post
    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. (Parent is QProcess(0x239e2e0), parent's thread is QThread(0x1e16850), current thread is clsThrTask(0x239e300)
    3. QObject: Cannot create children for a parent that is in a different thread.
    4. (Parent is QProcess(0x239e2e0), parent's thread is QThread(0x1e16850), current thread is clsThrTask(0x239e300)
    5. QObject: Cannot create children for a parent that is in a different thread.
    6. (Parent is QProcess(0x239e2e0), parent's thread is QThread(0x1e16850), current thread is clsThrTask(0x239e300)
    To copy to clipboard, switch view to plain text mode 
    You are probably passing "this" as the parent of an object that you create in "run()"

    Edit: ah, no, you are creating the QProcess during class initialization, making it an object run by the main thread. You need to create it in run() or sltProcPing().
    You should also be aware that sltPrintProcOut() is currently called by the main thread. If you want it called in the worker thread, you'll need to use a Qt::DirectConnection

    Cheers,
    _
    Last edited by anda_skoa; 15th January 2017 at 10:33.

  12. #10
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Talking Re: Call function of object within Threads.

    Hi, Anda Skoa.

    I modified
    Qt Code:
    1. QProcess * vprocPing = new QProcess ();
    To copy to clipboard, switch view to plain text mode 
    Who was in (* .h) to
    Qt Code:
    1. QProcess * vprocPing
    To copy to clipboard, switch view to plain text mode 
    and put
    Qt Code:
    1. vprocPing = new QProcess ();
    To copy to clipboard, switch view to plain text mode 
    In sltProcPing as you recommended. It did right stopped the messages in Application Output.
    Thanks, Thanks and Thanks.

    1ª) question
    When you said that sltPrintProcOut () is being called by the main thread and if I want it called in the worker thread, I'll need to use a "Qt:: DirectConnection", it was for I change the

    Qt Code:
    1. connect (vprocPing, SIGNAL(readyReadStandardOutput()), this, SLOT(sltPrintProcOut()));
    2. to
    3. connect (vprocPing, SIGNAL(readyReadStandardOutput()), this, SLOT(sltPrintProcOut()),Qt::DirectConnection);
    4. ???
    To copy to clipboard, switch view to plain text mode 

    2ª) question
    This would not cause problems with the Append method of the Response TextEdit, since some threads could call the method in question at the same time ???

  13. #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: Call function of object within Threads.

    Quote Originally Posted by marcos.miranda View Post
    1ª) question
    When you said that sltPrintProcOut () is being called by the main thread and if I want it called in the worker thread, I'll need to use a "Qt:: DirectConnection", it was for I change the

    Qt Code:
    1. connect (vprocPing, SIGNAL(readyReadStandardOutput()), this, SLOT(sltPrintProcOut()));
    2. to
    3. connect (vprocPing, SIGNAL(readyReadStandardOutput()), this, SLOT(sltPrintProcOut()),Qt::DirectConnection);
    4. ???
    To copy to clipboard, switch view to plain text mode 
    Yes, correct.

    Quote Originally Posted by marcos.miranda View Post
    2ª) question
    This would not cause problems with the Append method of the Response TextEdit, since some threads could call the method in question at the same time ???
    No, that is still fine. The connection between the thread object and the text edit is a Qt::AutoConnection, so it detects that sender and receiver are two different threads.

    Cheers,
    _

  14. #12
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Talking Re: Call function of object within Threads.

    Hi, Anda Skoa and everyone.

    How do I set the "vthrThread" thread array, defined by the real-time user, to go out of the local scope to its method and stay in the global scope of the class in question?

    It works like this.

    Qt Code:
    1. void MainWindow::on_cmdIniciar_clicked()
    2. {
    3.  
    4. Int vnumCont = ui->cboNumVezes->currentText().toInt();
    5.  
    6. clsThrTask *vthrThread[vnumCont];
    7.  
    8.  
    9. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    10. {
    11. vthrThread[vnumIni] = new clsThrTask(vstrIdThread,this);
    12.  
    13. QObject::connect(vthrThread[vnumIni], SIGNAL(sigRetorno(QString)),ui->txtResultado,SLOT(append(QString)));
    14. ....
    To copy to clipboard, switch view to plain text mode 

    I have tried it and it does not work.



    Qt Code:
    1. (*.h)
    2.  
    3. Private:
    4.  
    5. Int vnumCont;
    6. clsThrTask *vthrThread[];
    7.  
    8. (*.cpp)
    9.  
    10.  
    11. void MainWindow::on_cmdIniciar_clicked()
    12. {
    13.  
    14. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    15. {
    16. vthrThread[vnumIni] = new clsThrTask(vstrIdThread,this);
    17.  
    18. QObject::connect(vthrThread[vnumIni], SIGNAL(sigRetorno(QString)),ui->txtResultado,SLOT(append(QString))); // Erro Here "SIGSEGV"
    19. ....
    20.  
    21.  
    22. void MainWindow::on_cmdParar_clicked()
    23. {
    24.  
    25. for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
    26. {
    27. vthrThread[vnumIni].terminate();
    To copy to clipboard, switch view to plain text mode 

    Could you show me the correct form please?


    Oops: Sorry for my lack of education, Thanks for the previous explanation.
    Thanks.
    Last edited by marcos.miranda; 16th January 2017 at 12:12.

  15. #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: Call function of object within Threads.

    See the suggestion to use a container, e.g. a vector/QVector

    An array member would need to be a pointer to pointers, less obvious.

    And don't call QThread::terminate(), that is almost always a bad idea.
    Since your thread payload is event loop based, calling quit() should work.

    Cheers,
    _

  16. #14
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Call function of object within Threads.

    Hi, Anda Skoa and everyone

    On the Terminate () method, thanks for alerting me I took a look at QT Documentation.

    Now on QVector, I'm reviewing the QT Documentation but I have no idea how to declare this class and start its constructor.

    For simple examples I understood how to use it.

    Any code examples to help?

    Thanks.



    Added after 16 minutes:


    And before anyone says "You need to go back to your C ++ books and learn about variables and scoping. Your problem has nothing to do with everything and with basic C ++." I'm already doing this.
    Last edited by marcos.miranda; 16th January 2017 at 16:46.

  17. The following user says thank you to marcos.miranda for this useful post:

    d_stranz (17th January 2017)

  18. #15
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Talking Re: Call function of object within Threads.

    Hi, everyone.

    Thanks, d_stranz.

    I made the following change.

    Qt Code:
    1. In (* .h) I will declare.
    2.  
    3. QVector <clsThrTask> * vthrThread;
    4.  
    5. In (* .cpp) I did:
    6.  
    7. User informs the amount of threads through "vnumCont".
    8. VthrThread-> resize (vnumCont);
    9.  
    10. And how will you stay here?
    11.  
    12.  
    13. For (int vnumIni = 0; vnumIni <vnumCont; vnumIni ++)
    14. {
    15.  
    16. QString vstrIdThread = QString :: number (vnumIni); - // OK
    17.  
    18. VthrThread [vnumIni] = new clsThrTask (vstrIdThread, this); // ???? Erro Here.
    19. QObject :: connect (vthrThread [vnumIni], SIGNAL (sigRetorno (QString)), ui-> txtResult, SLOT (append (QString)); // ???? Erro Here.
    To copy to clipboard, switch view to plain text mode 

    Someone to help with the code ??

    Thanks.

  19. #16
    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: Call function of object within Threads.

    Almost

    Qt Code:
    1. QVector<clsThrTask*> vthrThread;
    To copy to clipboard, switch view to plain text mode 
    A vector to pointers of your thread class

    Cheers,
    _

  20. The following user says thank you to anda_skoa for this useful post:

    marcos.miranda (17th January 2017)

  21. #17
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Thumbs up Re: Call function of object within Threads.

    Hello everyone.

    I hope this little QT project on parallel processing will be the starting foot tip for many like me, beginners in C ++ and QT Framework.

    In this project we use some concepts about Threads (QThread), Processes (QProcess) and Vectors (QVector).

    I want to register a special thanks to Anda Skoa, D_Stranz and Edr567, without them it would not be possible to end this case study.

    I hope to count on you some other time.

    Thanks, Thanks and Thanks.

    The project will be available for download at the end of the post.
    Attached Files Attached Files

Similar Threads

  1. Replies: 5
    Last Post: 11th March 2015, 09:30
  2. Cannot call member function without object
    By ehntun in forum Qt Programming
    Replies: 0
    Last Post: 24th October 2012, 07:09
  3. How to call a function in the mother object from a child object?
    By Momergil in forum General Programming
    Replies: 4
    Last Post: 18th December 2011, 15:49
  4. Function call from a NULL this object ?
    By Computer Hater in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2011, 16:07
  5. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 14: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.