Results 1 to 8 of 8

Thread: How to run a QProcess in QThread without destroying the QThread

  1. #1
    Join Date
    Jul 2019
    Posts
    29
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default How to run a QProcess in QThread without destroying the QThread

    Hello! I don't get how to run a QProcess with QThread. Please help me, I tried also with both worker class and run() method but it doesn't work. I get: "QThread: desztroyed while thread is still running".
    Here is my code:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5. class mainWindow : public QWidget
    6. {
    7. Q_OBJECT
    8. //here are defined some variables
    9. public:
    10. mainWindow(QWidget *parent = 0);
    11. ~mainWindow();
    12. void createUI();
    13. void process();
    14. };
    15. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include <QWidget>
    2. #include <QPushButton>
    3. #include <QProcess>
    4. #include <QByteArray>
    5. #include <QTextCodec>
    6. #include <QString>
    7. #include <QDebug>
    8. #include "mainwindow.h"
    9.  
    10. mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
    11. {
    12. createUI();
    13. }
    14.  
    15.  
    16. mainWindow::~mainWindow()
    17. {
    18.  
    19. }
    20. void mainWindow::createUI(){
    21. //here I create the look of the window
    22. QPushButton buttonsearch = new QPushButton("Start process", this);
    23. buttonsearch->setToolTip("Start process");
    24. buttonsearch->setGeometry(200, 290, 100, 30);
    25. connect(buttonsearch, &QPushButton::clicked, [this]() {process(); });
    26. }
    27.  
    28. void mainWindow::process(){
    29. process->setProcessChannelMode(QProcess::MergedChannels);
    30. process->start("\"D:\\YTDownloader\\youtube-dl.exe\" -e --no-playlist https://www.youtube.com/watch?v=6V-wwfuxZxw");
    31. process->waitForReadyRead();
    32. QByteArray a = process->readAllStandardOutput();
    33. QTextCodec* utfCodec = QTextCodec::codecForName("UTF-8");
    34. QString processStdout = utfCodec->toUnicode(a);
    35. qDebug() << processStdout;
    36. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argl,char *argv[])
    5. {
    6. QApplication app(argl,argv);
    7.  
    8. mainWindow *window = new mainWindow();
    9. window->setWindowTitle("Test");
    10. window->setFixedSize(700, 335);
    11. window->show();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I try to run process() function into a thread when buttonsearch is clicked. Thank you for your help!

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

    Default Re: How to run a QProcess in QThread without destroying the QThread

    Why do you want to run QProcess with QThread? The former starts a new process, you don't need a thread for that.
    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. The following user says thank you to wysota for this useful post:

    INeedADollar (4th August 2019)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,553
    Thanks
    13
    Thanked 1,580 Times in 1,509 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to run a QProcess in QThread without destroying the QThread

    Quote Originally Posted by wysota View Post
    Why do you want to run QProcess with QThread?
    The example code does not use QThread and I think the OP is confusing a thread (managed by QThread) with another process (managed by QProcess)

    There seems to be an expectation that waitForReadyRead() does not return until all the process output is available: the call does not work like that, it returns when any of the process output is available. The code needs to either be reworked for a blocking mode of operation or to handle the output as it arrives between the start() call and the termination of the sub-process with slots connected to readyRead() etc. I suspect the latter option is better for a long-running download (like the example) while keeping a responsive UI.

    Perhaps INeedADollar can tell us more about the problem.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  5. The following user says thank you to ChrisW67 for this useful post:

    INeedADollar (4th August 2019)

  6. #4
    Join Date
    Jul 2019
    Posts
    29
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to run a QProcess in QThread without destroying the QThread

    Thanks for your response! I need the process in a thread because the window become unresponsive when I start the process.


    Added after 6 minutes:


    Thanks for your response! I didn't add the thread part because it's something bad in it and I thought someone can show me how to do it right but if you want I send you that part. I need to run the process in a thread because window become unresponsive when I start the process. In my example I try to get the title of videos from YouTube and I will also need a thread to download them, but I will do the download part later.
    Last edited by INeedADollar; 4th August 2019 at 12:19.

  7. #5
    Join Date
    Jul 2019
    Posts
    29
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to run a QProcess in QThread without destroying the QThread

    I solved it! Thank you for your response. Below is the code if someone is interested:
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5. class mainWindow : public QWidget
    6. {
    7. Q_OBJECT
    8. //here are defined some variables
    9. public:
    10. mainWindow(QWidget *parent = 0);
    11. ~mainWindow();
    12. void createUI();
    13. void process();
    14. QProcess *process = new QProcess(this);
    15.  
    16. private slots:
    17. void ReadOutput(int, QProcess::ExitStatus);
    18.  
    19. };
    20. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include <QWidget>
    2. #include <QPushButton>
    3. #include <QProcess>
    4. #include <QByteArray>
    5. #include <QTextCodec>
    6. #include <QString>
    7. #include <QDebug>
    8. #include "mainwindow.h"
    9.  
    10. mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
    11. {
    12. createUI();
    13. connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ReadOutput(int, QProcess::ExitStatus)));
    14. }
    15.  
    16.  
    17. mainWindow::~mainWindow()
    18. {
    19.  
    20. }
    21. void mainWindow::createUI(){
    22. //here I create the look of the window
    23. QPushButton buttonsearch = new QPushButton("Start process", this);
    24. buttonsearch->setToolTip("Start process");
    25. buttonsearch->setGeometry(200, 290, 100, 30);
    26. connect(buttonsearch, &QPushButton::clicked, [this]() {process(); });
    27. }
    28.  
    29. void mainWindow::process(){
    30. process->setProcessChannelMode(QProcess::MergedChannels);
    31. process->start("\"D:\\YTDownloader\\youtube-dl.exe\" -e --no-playlist https://www.youtube.com/watch?v=6V-wwfuxZxw");
    32.  
    33. void readOutput(int exitCode, QProcess::ExitStatus exitStatus){
    34. qDebug() << exitCode;
    35. qDebug() << exitStatus;
    36. QByteArray a = process->readAllStandardOutput();
    37. QTextCodec* utfCodec = QTextCodec::codecForName("UTF-8");
    38. processStdout = utfCodec->toUnicode(a);
    39. qDebug() << processStdout;
    40. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argl,char *argv[])
    5. {
    6. QApplication app(argl,argv);
    7.  
    8. mainWindow *window = new mainWindow();
    9. window->setWindowTitle("Test");
    10. window->setFixedSize(700, 335);
    11. window->show();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run a QProcess in QThread without destroying the QThread

    You don't have a thread here, you know...
    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.


  9. #7
    Join Date
    Jul 2019
    Posts
    29
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to run a QProcess in QThread without destroying the QThread

    Yes I know. I don't need the thread I was wrong.

  10. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run a QProcess in QThread without destroying the QThread

    In your original code your UI became unresponsive because you blocked the main thread in waitForReadyRead()

    In Qt most I/O is asynchronous for the very purpose of not blocking the main thread

    Also, you don't need a lambda here
    Qt Code:
    1. connect(buttonsearch, &QPushButton::clicked, [this]() {process(); });
    To copy to clipboard, switch view to plain text mode 
    you can directly connect to the process() function
    Qt Code:
    1. connect(buttonsearch, &QPushButton::clicked, this, &mainWindow::process);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    INeedADollar (8th August 2019)

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 15:49
  2. Starting QProcess from QThread
    By sandeep.theartist in forum Qt Programming
    Replies: 2
    Last Post: 7th June 2012, 07:18
  3. QThread to run one or more QProcess
    By smhall316 in forum Newbie
    Replies: 0
    Last Post: 27th September 2011, 21:39
  4. QThread and QProcess problem
    By codebehind in forum Qt Programming
    Replies: 13
    Last Post: 7th August 2007, 09:11
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16:52

Tags for this Thread

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.