I'm trying to reimplement a closeEvent handler but is not working as i want. I hope you can help me. I need start a python program as a last "task" of my main program. Here my first attempt.
up->start( uploaderPath );
wd->accept();
}
void MainWindow::closeEvent(QCloseEvent *wd) {
QString uploaderPath = "python " + QCoreApplication::applicationDirPath( ) + "/Uploader/setter.py";
QProcess *up = new QProcess( this );
up->start( uploaderPath );
wd->accept();
}
To copy to clipboard, switch view to plain text mode
Error message when i close my main program:
QProcess: Destroyed while process ("python") is still running.
So, i tried to create a second thread where i could run this python program. I thought this program would run regardless of my main program. I close my main program, since the second(python) will run in another thread he will do what he should and will die too. But i was wrong. Here the code.
Second attempt.
#ifndef RESETTER_H
#define RESETTER_H
#include <QtCore>
public:
Resetter( );
void run( );
};
#endif // RESETTER_H
#ifndef RESETTER_H
#define RESETTER_H
#include <QtCore>
class Resetter : public QThread {
public:
Resetter( );
void run( );
};
#endif // RESETTER_H
To copy to clipboard, switch view to plain text mode
#include "resetter.h"
Resetter::Resetter( ) { }
void Resetter::run( ) {
up->start( uploaderPath );
}
#include "resetter.h"
Resetter::Resetter( ) { }
void Resetter::run( ) {
QString uploaderPath = "python " + QCoreApplication::applicationDirPath( ) + "/Uploader/setter.py";
QProcess *up = new QProcess( this );
up->start( uploaderPath );
}
To copy to clipboard, switch view to plain text mode
Resetter rs;
rs.start( );
wd->accept();
}
void MainWindow::closeEvent(QCloseEvent *wd) {
Resetter rs;
rs.start( );
wd->accept();
}
To copy to clipboard, switch view to plain text mode
Error message when i close my main program:
QThread: Destroyed while thread is still running
Segmentation fault (core dumped)
Or create that object on the heap:
Resetter *rs = new Resetter( );
rs->start( );
wd->accept();
}
void MainWindow::closeEvent(QCloseEvent *wd) {
Resetter *rs = new Resetter( );
rs->start( );
wd->accept();
}
To copy to clipboard, switch view to plain text mode
Error message when i close my main program:
QCoreApplication::applicationDirPath: Please instantiate the QApplication object first
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0x112e700), parent's thread is QThread(0x1098e60), current thread is QThread(0x112e700)
Bookmarks