Results 1 to 8 of 8

Thread: Threads in GUI application

  1. #1
    Join Date
    Jan 2006
    Location
    Saint-Petersburg (Russia)
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Threads in GUI application

    Hi all!
    Just want to write to the textarea of main application with threads.
    Implemeth Thread class:

    #include <QTextEdit>
    #include <QThread>

    class Worker : public QThread
    {
    Q_OBJECT

    public:
    void run();
    void connectTextOutput(QTextEdit *te);
    Worker();
    ~Worker();

    private:
    QTextEdit *text;
    QString q;
    };

    and realization:

    void Worker::run()
    {
    text->insertPlainText(q + "AND time is come true\n");
    q += "22";
    exec();
    }

    void Worker::connectTextOutput(QTextEdit *te)
    {
    text = te;

    }

    And in Main application constructor:

    w = new Worker();
    w->connectTextOutput(ui.textLog);
    w->start();

    So...

    A get one string writed to text area.
    Threat not workedd..
    Whats wrong ?
    Succes is 5% of talent and 95% of work!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    You can't access widgets from a non-GUI thread. All you can do is to send them a signal through a queued connection.

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

    blackliteon (12th April 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    I don't know about Windows and Mac, but when I've tried to paint (via QPainter) from non-gui thread, I had a lot of X11 errors. Maybe it's because my x.org API is not reentrant.

  5. #4
    Join Date
    Jan 2006
    Location
    Saint-Petersburg (Russia)
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    Thanks!
    Try this today!
    Succes is 5% of talent and 95% of work!

  6. #5
    Join Date
    Jan 2006
    Location
    Saint-Petersburg (Russia)
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    Quote Originally Posted by jacek
    You can't access widgets from a non-GUI thread. All you can do is to send them a signal through a queued connection.
    What I need to do to make my Thred class - GUI class ?
    Succes is 5% of talent and 95% of work!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    Quote Originally Posted by blackliteon
    What I need to do to make my Thred class - GUI class ?
    You can't do this. There can be only one GUI thread (i.e. the one which created the QApplication instance).

  8. #7
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    Quote Originally Posted by AlexKiriukha
    I don't know about Windows and Mac, but when I've tried to paint (via QPainter) from non-gui thread, I had a lot of X11 errors. Maybe it's because my x.org API is not reentrant.
    Did you read the documentation on QThread?
    Save yourself some pain. Learn C++ before learning Qt.

  9. #8
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads in GUI application

    Quote Originally Posted by Chicken Blood Machine
    Did you read the documentation on QThread?
    Sure. I know that application can have only one GUI thread but experiments (even if they are wrong) is interesting thing :-)

Similar Threads

  1. Qt Application + Web Integration -- Help needed
    By swamyonline in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2009, 11:59
  2. Sharing data between threads
    By bbui210 in forum Qt Programming
    Replies: 15
    Last Post: 19th October 2008, 17:56
  3. Start Qt application as Windows Service
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 10th May 2008, 17:23
  4. dll + application
    By fpujol in forum Qt Programming
    Replies: 11
    Last Post: 15th April 2007, 18:37

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.