Results 1 to 9 of 9

Thread: QThread locks my gui ??

  1. #1
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread locks my gui ??

    Hello friends,

    When I click my Gui B button it starts a QThread.

    Qt Code:
    1. class CheckFile : public QThread
    2. //**************************************************************************
    3. {
    4. Q_OBJECT
    5. public:
    6. CheckFile(const QString &p_inFile,
    7. const QString &p_notInsertedFile,
    8. QObject* parent = 0);
    9. virtual ~CheckFile() { }
    10. public slots:
    11. protected:
    12. void run();
    13. private:
    14. QString qstr_inFile;
    15. QString qstr_notInsertedFile;
    16. private slots:
    17. signals:
    18.  
    19. void setText(const QString &);
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    in the implementation I parse a file and emit for every line a settext like this

    Qt Code:
    1. void CheckFile::run()
    2. {
    3. QString str;
    4. ...
    5. emit setText(str);
    6. }
    To copy to clipboard, switch view to plain text mode 

    In the Slot function for my button clicked event I declare my connection for the Thread :

    Qt Code:
    1. connect(thr_CheckFile, SIGNAL(setText(const QString& )), bodyEdit, SLOT(append(const QString&)));
    To copy to clipboard, switch view to plain text mode 

    after the text are append to my Qtextedit in my gui my window freeze that means I can not do anything and the memory using grows up

    Any Suggestions ??

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread locks my gui ??

    Hi,

    How do you start the QThread?
    Òscar Llarch i Galán

  3. #3
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread locks my gui ??

    Like this:

    Qt Code:
    1. thr_CheckFile = new CheckFile(selectedItemFile,
    2. notInsertedFile,
    3. this);
    4.  
    5. connect(thr_CheckFile, SIGNAL(setText(const QString& )), bodyEdit, SLOT(append(const QString&)));
    6. thr_CheckFile->start();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread locks my gui ??

    Hi,

    Maybe the SLOT "append" is reciving too much data every time the SIGNAL is emmited.
    Try using a QString variable and append the data into it. When the QThread finish you can set the text to the QTextEdit.
    Òscar Llarch i Galán

  5. #5
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread locks my gui ??

    Hmm I think I am not sure if I understand you.

    I use a QString as parameter.... ?????

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread locks my gui ??

    Hi,

    Qt Code:
    1. connect(thr_CheckFile, SIGNAL(setText(const QString&)), this, SLOT(appendString(const QString&)));
    2.  
    3. //Connect the Thread Finished signal
    4. connect(thr_CheckFile, SIGNAL(finished()),this,SLOT(threadFinished())));
    5.  
    6. ...
    7. void yourClass::appendString(const QString& qString)
    8. {
    9. //QString m_qString; //Declared on yourClass
    10. m_qString.append(qString);
    11. }
    12. void yourClass::threadFinished()
    13. {
    14. //When the Thread is finished we have all the data that want to show to QTextEdit
    15. bodyEdit.setText(m_qString);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Something like this maybe will help you.
    Òscar Llarch i Galán

  7. #7
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread locks my gui ??

    Ok thank you....

    but I think I have to implement a Progressbar for big files ... ;O)))

  8. #8
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QThread locks my gui ??

    Hi,

    I'm not an expert in Qt multi-threading but I think your problem comes from your connect() parameters. You try to send a signal from one thread to the gui's one, this can be dangerous if you don't take care of it.

    When you send a signal from a thread to an other, try to use something like this :
    Qt Code:
    1. connect(thr_CheckFile, SIGNAL(setText(const QString&)), this, SLOT(appendString(const QString&)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    This will post the signal in the signal queue of the gui thread, and then, thread will manage it later as any other event from the GUI.

    I already had the same problem as you, where a thread sends some signals to the gui in order to make a progress bar raise, with the Qt::QueuedConnection it's works fine. A piece of advice : I had some stange troubles with the connect() function when I used it in a thread, now I always specify the fifth parameters in this case (even if the default should work) and I don't have any surprises anymore.

    I hope it can solve the locks of your GUI.

  9. #9
    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 locks my gui ??

    You don't need to use any threads here, use QProgressDialog or something similar.

Similar Threads

  1. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2008, 17:43
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  4. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22: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.