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
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
{
Q_OBJECT
//here are defined some variables
public:
~mainWindow();
void createUI();
void process();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
class mainWindow : public QWidget
{
Q_OBJECT
//here are defined some variables
public:
mainWindow(QWidget *parent = 0);
~mainWindow();
void createUI();
void process();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include <QWidget>
#include <QPushButton>
#include <QProcess>
#include <QByteArray>
#include <QTextCodec>
#include <QString>
#include <QDebug>
#include "mainwindow.h"
{
createUI();
}
mainWindow::~mainWindow()
{
}
void mainWindow::createUI(){
//here I create the look of the window
buttonsearch->setToolTip("Start process");
buttonsearch->setGeometry(200, 290, 100, 30);
connect(buttonsearch, &QPushButton::clicked, [this]() {process(); });
}
void mainWindow::process(){
process
->setProcessChannelMode
(QProcess::MergedChannels);
process->start("\"D:\\YTDownloader\\youtube-dl.exe\" -e --no-playlist https://www.youtube.com/watch?v=6V-wwfuxZxw");
process->waitForReadyRead();
QString processStdout
= utfCodec
->toUnicode
(a
);
qDebug() << processStdout;
}
#include <QWidget>
#include <QPushButton>
#include <QProcess>
#include <QByteArray>
#include <QTextCodec>
#include <QString>
#include <QDebug>
#include "mainwindow.h"
mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{
createUI();
}
mainWindow::~mainWindow()
{
}
void mainWindow::createUI(){
//here I create the look of the window
QPushButton buttonsearch = new QPushButton("Start process", this);
buttonsearch->setToolTip("Start process");
buttonsearch->setGeometry(200, 290, 100, 30);
connect(buttonsearch, &QPushButton::clicked, [this]() {process(); });
}
void mainWindow::process(){
process->setProcessChannelMode(QProcess::MergedChannels);
process->start("\"D:\\YTDownloader\\youtube-dl.exe\" -e --no-playlist https://www.youtube.com/watch?v=6V-wwfuxZxw");
process->waitForReadyRead();
QByteArray a = process->readAllStandardOutput();
QTextCodec* utfCodec = QTextCodec::codecForName("UTF-8");
QString processStdout = utfCodec->toUnicode(a);
qDebug() << processStdout;
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argl,char *argv[])
{
mainWindow *window = new mainWindow();
window->setWindowTitle("Test");
window->setFixedSize(700, 335);
window->show();
return app.exec();
}
#include <QApplication>
#include "mainwindow.h"
int main(int argl,char *argv[])
{
QApplication app(argl,argv);
mainWindow *window = new mainWindow();
window->setWindowTitle("Test");
window->setFixedSize(700, 335);
window->show();
return app.exec();
}
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!
Bookmarks