Results 1 to 8 of 8

Thread: QThread Data Access Method

  1. #1
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4

    Default QThread Data Access Method

    Hi all:

    Just got a basic question concerning QThread. Say if my QThread Object is inside another inherited QMainwindow which also contains other non-Qt classes. If i start a the thread, what should i do to make the run() function of QThread to have access to the members in the calling inherited QMainwindow class? Thanks in advance!

    schan117

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread Data Access Method

    call a public fuction in the thread class
    ex:
    Qt Code:
    1. public:
    2. void startThread();
    3. void run();
    4.  
    5. in void startThread()
    6. {
    7. if(!isRunning()){
    8. start();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    so in the mainwindow class call
    thread->startThread();

    and it will automatically call the run() of the thread()
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread Data Access Method

    pass the thread's constructor a pointer to the data you want to access.
    Make sure you guard (i.e. lock) against read/write conflicts when accessing the same data in different threads. Also note that QWidgets may only be accessed from the gui thread.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread Data Access Method

    Quote Originally Posted by schan117 View Post
    If i start a the thread, what should i do to make the run() function of QThread to have access to the members in the calling inherited QMainwindow class? Thanks in advance!
    You shouldn't do that because widget classes are not thread safe.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4

    Default Re: QThread Data Access Method

    Hi all:

    Thanks for the answers guys. So this means I can only pass to the thread a QWidget class but not some other classes? I am asking this because I want to QThread to access to the camera object of parent class to do capturing and display it on QLabel. Another way I think of is to use QTimer. Whats your ideas guys? Thanks!

    schan117

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread Data Access Method

    No, QWidget is not thread-safe as well. You musn't call any of QWidget (or descendant) methods from within a worker thread. If you want to transfer data between the worker thread and the main thread, you have to use either events or signals.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4

    Default Re: QThread Data Access Method

    So you mean what i should do is to setup the thread to emit a signal when inside run() of the QThread Class and connect it back to a slot of the parent class and from that access the data?

    schan117

  8. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread Data Access Method

    follow this example ... it suits exact requirement for u on QThread
    http://doc.trolltech.com/4.0/network...uneclient.html
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. Struggling with QThread...
    By TemporalBeing in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2009, 20:46
  2. Replies: 2
    Last Post: 18th December 2008, 07:43
  3. Calling Recursivly loading function in Run() method of QThread
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 14:42
  4. Replies: 2
    Last Post: 27th March 2007, 12:09
  5. IO Control Mechanism (Hardware Data Access)
    By rud_1023 in forum General Programming
    Replies: 2
    Last Post: 23rd October 2006, 12:50

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.