Results 1 to 3 of 3

Thread: QThread and method with pointer argument

  1. #1
    kaszewczyk Guest

    Default QThread and method with pointer argument

    Hello, my problem is:
    for example i have a class
    Qt Code:
    1. class FooClass{
    2. public:
    3. FooClass();
    4. ~FooClass();
    5.  
    6. private:
    7. QTextEdit* text;
    8. };
    9. FooClass::FooClass(){
    10. text = new QTextEdit;
    11. }
    To copy to clipboard, switch view to plain text mode 
    and now i want to make thread where i can put something in my FooClass::text fild
    Qt Code:
    1. class FooThread : public QThread{
    2. public:
    3. void setTextEdit(QTextEdit* __p);
    4.  
    5. protected:
    6. void run()
    7.  
    8. private:
    9. QTextEdirt* textPointer;
    10. };
    11. void FooThread::setTextEdit(QTextEdit* __p){
    12. this->textPointer = __p
    13. }
    14.  
    15. void FooThread::run(){
    16. this->textPointer->insertPlainText("Hello from QThred");
    17. }
    To copy to clipboard, switch view to plain text mode 
    my aim is to enter ""Hello from QThred" string into FooClass TextEdit
    Qt Code:
    1. FooClass::FooClass(){
    2. text = new QTextEdit;
    3. FooThread *thread = new FooThread;
    4. thread->setTextEdit(this->text);
    5. thread->start();
    6. }
    To copy to clipboard, switch view to plain text mode 
    but when i do it like this i get runtime error :/

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

    Default Re: QThread and method with pointer argument

    You can't access widgets from within worker threads. Use signals instead to communicate with the main thread and change the contents of the text edit from within there.
    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.


  3. #3
    Join Date
    Feb 2008
    Posts
    50
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: QThread and method with pointer argument

    Emit a signal in the thread`s run() maybe with some QString argument (the text which you want) and catch the signal with a proprietary slot in the main thread which has the same QString argument to get the text and change the text of the widget in that slot.

Similar Threads

  1. QThread Data Access Method
    By schan117 in forum Qt Programming
    Replies: 7
    Last Post: 18th August 2009, 07:25
  2. Calling Recursivly loading function in Run() method of QThread
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:42
  3. Replies: 2
    Last Post: 27th March 2007, 13:09

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
  •  
Qt is a trademark of The Qt Company.