[gelöst] QThreads und Event Loops
Verfasst: 2. April 2010 20:29
Hallo,
ich sitz' zur Zeit an einem simplen Konsolenprogramm, dass aus main() und einer von QThread abgeleiteten Klasse besteht. in main() werden Argumente verarbeitet und dann wird der Thread erzeugt. Im Moment kann ich jedoch im Thread keine Funktionen wie etwa quit() benutzen, bzw. sie bewirken nichts.
Damit soll erstmal einfach ein rar-file entpackt werden und die Ausgabe weitergegeben werden. Ich erhalte zum Schluss die Ausgabe:
Hat der Thread überhaupt eine EventLoop, oder würden die SLOTS ohne EventLoop erst garnicht funktionieren?
Woran liegt es sonst, dass nicht beendet wird?
Danke im Voraus.
ich sitz' zur Zeit an einem simplen Konsolenprogramm, dass aus main() und einer von QThread abgeleiteten Klasse besteht. in main() werden Argumente verarbeitet und dann wird der Thread erzeugt. Im Moment kann ich jedoch im Thread keine Funktionen wie etwa quit() benutzen, bzw. sie bewirken nichts.
Code: Alles auswählen
// main.cpp
#include <QtCore>
#include <QCoreApplication>
#include <thread.h>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a( argc, argv );
if( argc == 1 )
return 1;
QString file;
file = argv[1];
thread *thread1 = new thread( file );
thread1->start();
return a.exec();
}Code: Alles auswählen
// thread.h
#ifndef THREAD_H_
#define THREAD_H_
#include <QThread>
#include <QString>
#include <QCoreApplication>
class QProcess;
class thread: public QThread {
Q_OBJECT
private:
QProcess *process;
private slots:
void getOutput();
public:
thread( QString file, QObject *parent = 0);
virtual ~thread();
protected:
void run();
};
#endif /* THREAD_H_ */Code: Alles auswählen
// thread.cpp
#include "thread.h"
#include <QObject>
#include <QProcess>
#include <iostream>
#include <QtCore>
#include <QCoreApplication>
using namespace std;
thread::thread( QString file, QObject *parent){
QObject *myParent = new QObject();
process = new QProcess( myParent );
connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(getOutput()) );
QStringList strList;
strList << "e" << file;
process->start( "unrar", strList );
}
void thread::run(){
exec();
}
void thread::getOutput(){
QString output = process->readAll();
cout << output.toStdString() << endl;
if( output.contains( "All OK") ){
cout << "exit..." << endl;
process->kill();
quit();
}
}
thread::~thread() {
}Das Programm beendet sich jedoch nicht, obwohl es das if-statement erreicht worin das Programm beendet werden soll.All OK
exit...
Hat der Thread überhaupt eine EventLoop, oder würden die SLOTS ohne EventLoop erst garnicht funktionieren?
Woran liegt es sonst, dass nicht beendet wird?
Danke im Voraus.