Results 1 to 9 of 9

Thread: How to use the thread class with the QProcess?

  1. #1
    Join Date
    Jan 2011
    Posts
    40
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to use the thread class with the QProcess?

    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.
    Qt Code:
    1. class thread_main : public QThread
    2. {
    3. public:
    4. QString cmd_name;
    5. QStringList cmd_list;
    6. QProcess mProcess;
    7. thread_main(QString cmd_name,QStringList cmd_list);
    8. virtual void run();
    9. ~thread_main();
    10. };
    11.  
    12. void thread_main::run()
    13. {
    14. cmd_list.replaceInStrings(QString("/"),QString("\\"));
    15. mProcess->start(cmd_name,cmd_list);
    16. };
    17.  
    18. //i use the thread_main class i defined to start a new process;
    19. thread_main *a= new thread_main(cmd_name,cmd_list);
    20. a->start();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use the thread class with the QProcess?

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    40
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use the thread class with the QProcess?

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use the thread class with the QProcess?

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2011
    Posts
    40
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use the thread class with the QProcess?

    Qt Code:
    1. void load_test::un7z_htmlfiles(QString filename, QString filepath)
    2. {
    3. QProcess mProcess;
    4. QString cmd_name;
    5. QStringList cmd_line;
    6. cmd_name="7z.exe";
    7. cmd_line.clear();
    8. cmd_line.append("e");
    9. cmd_line.append("-ir!"+filename+"q.htm");
    10. cmd_line.append("-o./data/tmp/");
    11. cmd_line.append("./data/qsource.db");
    12. cmd_line.append("-y");
    13. cmd_line.replaceInStrings(QString("/"),QString("\\"));
    14. //mProcess.execute(cmd_name,cmd_line);
    15. mProcess.startDetached(cmd_name,cmd_line);
    16. };
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use the thread class with the QProcess?

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    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: How to use the thread class with the QProcess?

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


  8. #8
    Join Date
    Jan 2011
    Posts
    40
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use the thread class with the QProcess?

    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?

  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: How to use the thread class with the QProcess?

    No, it doesn't work.
    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.


Similar Threads

  1. run QProcess in a thread
    By bigbill in forum Newbie
    Replies: 6
    Last Post: 15th December 2010, 11:45
  2. Is a QProcess thread safe in Qt4?
    By Jay_D in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2009, 16:38
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. calling Gui treeWidget in Thread class
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2007, 09:14
  5. How i will use treeWidget of GUI class in thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 15:31

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.