PDA

View Full Version : QThread locks my gui ??



LordQt
5th December 2008, 09:22
Hello friends,

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



class CheckFile : public QThread
//************************************************** ************************
{
Q_OBJECT
public:
CheckFile(const QString &p_inFile,
const QString &p_notInsertedFile,
QObject* parent = 0);
virtual ~CheckFile() { }
public slots:
protected:
void run();
private:
QString qstr_inFile;
QString qstr_notInsertedFile;
private slots:
signals:

void setText(const QString &);

};


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



void CheckFile::run()
{
QString str;
...
emit setText(str);
}


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



connect(thr_CheckFile, SIGNAL(setText(const QString& )), bodyEdit, SLOT(append(const QString&)));


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 ??

^NyAw^
5th December 2008, 09:36
Hi,

How do you start the QThread?

LordQt
5th December 2008, 09:42
Like this:



thr_CheckFile = new CheckFile(selectedItemFile,
notInsertedFile,
this);

connect(thr_CheckFile, SIGNAL(setText(const QString& )), bodyEdit, SLOT(append(const QString&)));
thr_CheckFile->start();

^NyAw^
5th December 2008, 09:48
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.

LordQt
5th December 2008, 09:53
Hmm I think I am not sure if I understand you.

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

^NyAw^
5th December 2008, 09:58
Hi,



connect(thr_CheckFile, SIGNAL(setText(const QString&)), this, SLOT(appendString(const QString&)));

//Connect the Thread Finished signal
connect(thr_CheckFile, SIGNAL(finished()),this,SLOT(threadFinished())));

...
void yourClass::appendString(const QString& qString)
{
//QString m_qString; //Declared on yourClass
m_qString.append(qString);
}
void yourClass::threadFinished()
{
//When the Thread is finished we have all the data that want to show to QTextEdit
bodyEdit.setText(m_qString);
}


Something like this maybe will help you.

LordQt
5th December 2008, 13:08
Ok thank you....

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

nix
8th December 2008, 17:44
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 :

connect(thr_CheckFile, SIGNAL(setText(const QString&)), this, SLOT(appendString(const QString&)), Qt::QueuedConnection);

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.

wysota
8th December 2008, 19:53
You don't need to use any threads here, use QProgressDialog or something similar.