Results 1 to 8 of 8

Thread: QThread Question

  1. #1
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QThread Question

    Hello!

    I am new to Qt, my question may seem simple, but I searched all over the Internet and didn't find the right answer.

    I have a QThread class:

    Qt Code:
    1. #include "thread.h"
    2.  
    3. Thread::Thread(QObject *parent)
    4. : QThread(parent)
    5. {
    6. }
    7.  
    8. void Thread::run(string url)
    9. {
    10. ui->webView->setUrl(QUrl(url.c_str()));
    11.  
    12. connect(ui->webView, SIGNAL(loadFinished(bool)), SLOT(onLoadFinished(bool)));
    13. connect(ui->webView, SIGNAL(loadProgress(int)), SLOT(onLoadProgress(int)));
    14. //connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
    15.  
    16. exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Of course, this code doesn't work because I can't access ui from my main class.

    So the question is: How can I access the widgets in my main class from this class?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QThread Question

    You can use events or signals and slots.

    But be careful with defining slots. Creating slots in a QThread subclass and calling them doesn't always run them in the thread.
    I think the best approach is to create a QObject subclass and move the QObject object to a thread instead of subclassing QThread directly.

  3. #3
    Join Date
    Jan 2011
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QThread Question

    Another way is by giving the Thread class pointer to the widget with ui for example during construction:

    Qt Code:
    1. Thread::Thread(QObject *parent, QWidget *window)
    2. : QThread(parent),
    3. pointerToWindowWithUi(window)
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

    where pointerToWindowWithUi (xD) is thread class member.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QThread Question

    Quote Originally Posted by karlkar View Post
    Another way is by giving the Thread class pointer to the widget with ui for example during construction:

    Qt Code:
    1. Thread::Thread(QObject *parent, QWidget *window)
    2. : QThread(parent),
    3. pointerToWindowWithUi(window)
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

    where pointerToWindowWithUi (xD) is thread class member.
    That breaks the principle that every UI object should remain in one thread only.

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

    Quote Originally Posted by tbscope View Post
    That breaks the principle that every UI object should remain in one thread only.
    Not in one thread, in one particular specific thread that is ran by the QApplication object.
    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.


  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QThread Question

    So the question is: How can I access the widgets in my main class from this class?
    You can't, they are typically private for a reason. As others have said, you can use signals/slots to transfer pieces of information from one class to another, even across thread boundaries. For example:
    • The main GUI class exposes a slot that can be used to set the URL, and the two signals related to its private QWebview.
    • The Thread class exposes a signal that requests a URL change, and the two slots you already have.
    • When you create instance of the Thread class connect the slots to the signals. This happens outside the Thread class.

    If you intend grabbing the content of the web view and processing it in the thread then you should probably rethink the design.

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread Question

    read this, it should help.
    See also.

  8. #8
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread Question

    Thank you all for you answears!

Similar Threads

  1. Replies: 2
    Last Post: 29th July 2010, 06:49
  2. QTimer / QThread question
    By Galen in forum Newbie
    Replies: 13
    Last Post: 20th April 2010, 22:39
  3. basic qthread question
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2008, 22:43
  4. Replies: 4
    Last Post: 26th June 2008, 19:41
  5. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 15:38

Tags for this Thread

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.