Results 1 to 9 of 9

Thread: pthread instead QThread

  1. #1
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post pthread instead QThread

    I need to develop three programs, and they should use some pthreads, I can't use QThread cause theacher said to use native threads.
    The first program is a PLC simulator with:

    10 analogic in;
    10 analogic out;
    10 digital in;
    10 digital out;

    I think I can use a pthread to create the "in" random values, but when I call the method to set the new values in QSpinBox, the frontend freezes!!
    Using cout, I see that the values are been generated, and some X errors messages are shown, some have this: "(RENDER)". I think is just a render problem, but I don't know what can solve it!
    Unfortunately, I don't have the source in these computer I'm using. I'll post som sources later, but someone have an ideia of what is happening??

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: pthread instead QThread

    You must not touch the GUI in a worker thread.
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pthread instead QThread

    ????????

    I didn't understand.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: pthread instead QThread

    From what I understood you call some spinbox method directly from a worker thread. This is not acceptable.

    From Qt docs:
    Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: pthread instead QThread

    In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished. This is the approach used for implementing the Mandelbrot and the Blocking Fortune Client example.
    I suggest you carefully read Thread support in Qt
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pthread instead QThread

    Ok I understood.

    I should use mutex, semaphore or something to make main thread set the values right?

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: pthread instead QThread

    yes, you can't call GUI methods in another thread. use signals/slots or custom event for communication between main and worker thread.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pthread instead QThread

    Ok

    I've implemented custom signal/slots:

    Qt Code:
    1. #ifndef PORTA_H
    2. #define PORTA_H
    3.  
    4. #include <QObject>
    5.  
    6. class Porta : public QObject{
    7.  
    8. public:
    9. static const int DISCRETE=0;
    10. static const int BINARY=1;
    11.  
    12. Porta ();
    13. int getValue ();
    14. virtual void setType (int t);
    15.  
    16. protected:
    17. int value;
    18. pthread_t thread;
    19. int type;
    20.  
    21. int createThread ();
    22. static void *startThread (void *obj);
    23. virtual void threadFunction(void)=0;
    24.  
    25. signals:
    26. void valueChanged();
    27.  
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "PortaEntrada.h"
    2.  
    3. #include <iostream>
    4.  
    5. using namespace std;
    6.  
    7. PortaEntrada::PortaEntrada(){
    8. oscilation=BINARY;
    9. }
    10.  
    11. void PortaEntrada::threadFunction(void){
    12. while(1){
    13. doGenerate();
    14. sleep(5);
    15. }
    16. }
    17.  
    18. int PortaEntrada::doGenerate(){
    19. value=generator->generate();
    20. emit valueChanged();
    21. }
    22.  
    23. void PortaEntrada::setOscilation (int o){
    24. oscilation=(o%4)+1;
    25. if(type==BINARY && oscilation!=BINARY){
    26. oscilation=BINARY;
    27. }
    28. switch(oscilation){
    29. case BINARY : generator=new (BGenerator);
    30. break;
    31. case QUADRATIC : generator=new (QGenerator);
    32. break;
    33. case TRIANGULAR : generator=new (TGenerator);
    34. break;
    35. case CONSTANT : generator=new (CGenerator);
    36. break;
    37. }
    38. createThread();
    39. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "SpinView.h"
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6. SpinView::SpinView(QWidget *parent):QSpinBox(parent){
    7. }
    8.  
    9.  
    10. void SpinView::setModel(Porta *m){
    11. View::setModel(m);
    12. connect(m,SIGNAL(valueChanged()),this,SLOT(setInView()));
    13. }
    14.  
    15. void SpinView::setInValue(void){
    16. setValue(model->getValue());
    17. }
    To copy to clipboard, switch view to plain text mode 


    but when I run the program:

    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin1')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin1')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin1')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin6')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin2')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin2')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin2')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin7')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin3')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin3')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin3')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin8')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin4')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin4')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin4')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin9')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin5')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin5')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin5')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin10')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin6')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin6')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin6')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin1')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin7')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin7')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin7')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin2')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin8')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin8')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin8')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin3')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin9')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin9')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin9')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin4')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dInSpin10')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aInSpin10')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'dOutSpin10')
    Object::connect: No such signal QObject::valueChanged()
    Object::connect: (receiver name: 'aOutSpin5')
    How can I connect this custom signal with my custom slot??

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: pthread instead QThread

    Class Porta is missing the required Q_OBJECT macro.

    PS. Please do not double post! Thread closed.
    Last edited by jpn; 23rd December 2008 at 08:23. Reason: updated contents
    J-P Nurmi

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 12:51
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 14:06
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 13:54
  4. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 08:51
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 23:51

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.