PDA

View Full Version : How to use the thread class with the QProcess?



wter27
25th February 2011, 11:34
what is wrong with my code?If I don't use the QThread class ,the QProcess works fine.After I put the code of QProcess into the thread_main run() part. the QProcess could not start.

class thread_main : public QThread
{
public:
QString cmd_name;
QStringList cmd_list;
QProcess mProcess;
thread_main(QString cmd_name,QStringList cmd_list);
virtual void run();
~thread_main();
};

void thread_main::run()
{
cmd_list.replaceInStrings(QString("/"),QString("\\"));
mProcess->start(cmd_name,cmd_list);
};

//i use the thread_main class i defined to start a new process;
thread_main *a= new thread_main(cmd_name,cmd_list);
a->start();

high_flyer
25th February 2011, 11:41
Why are you putting the QProcess object iin another thread?
QProcess will run your process in a new thread - so you don't need to worry about it.

Regarding your code:
Your QProcess object doesn't have the thread affinity you think it has, it has the thread affinity of the thread in which your QThread object was created.
Only objects that are created in the run() method have your new threads affinity, or if you explicitly call moveToThread().

But you don't need to bother with threads in your case.

wter27
25th February 2011, 14:01
Because when I use the QProcess to call the external executives ,UI always being to fake death.Only after the QProcess finished call the external exe programs,the UI became normal. The QProcess startDetached can not meet my demands.And someone told me that I could use the thread class to get it.
Thanks very much for your reply .But I am not understanding what you just mentioned very well.So could you just give a small example or a link?Thank you again.

high_flyer
25th February 2011, 14:12
Because when I use the QProcess to call the external executives ,UI always being to fake death.
Please show your code where you were using QProcess.

wter27
25th February 2011, 15:12
void load_test::un7z_htmlfiles(QString filename, QString filepath)
{
QProcess mProcess;
QString cmd_name;
QStringList cmd_line;
cmd_name="7z.exe";
cmd_line.clear();
cmd_line.append("e");
cmd_line.append("-ir!"+filename+"q.htm");
cmd_line.append("-o./data/tmp/");
cmd_line.append("./data/qsource.db");
cmd_line.append("-y");
cmd_line.replaceInStrings(QString("/"),QString("\\"));
//mProcess.execute(cmd_name,cmd_line);
mProcess.startDetached(cmd_name,cmd_line);
};

when i use the QProcess to call the 7za.exe to extract the compressed files.It acts very slowly.Only after the 7za.exe finished the job.The ui became normal.

high_flyer
25th February 2011, 15:26
You don't need to declare the 'mProcess' variable, if you are suing the static methods.

It acts very slowly.Only after the 7za.exe finished the job.The ui became normal.
This is important: does it only get very slow, or does it freeze?
Do other windows on your system during the time the process is running run normally, or also slow?
If its only slow(but not frozen), then its due to the load on the CPU - it IS running in another thread.

However if the GUI freezes, then there is some other problem, which based on the information you have given so far, I can't tell what it might be.

wysota
25th February 2011, 15:35
Next time when you provide code, make sure it is the real code you have and that it compiles. The snippet you posted here will not compile.

wter27
28th February 2011, 02:20
I will remember this next time.

Next time when you provide code, make sure it is the real code you have and that it compiles. The snippet you posted here will not compile.
I have fix the problem.The reason is that I get the wrong variaties.
And anthor problem is how to use the UI class in the QTHREAD,does it work?

wysota
28th February 2011, 09:35
No, it doesn't work.