
Originally Posted by
norobro
Why not connect the signal QProcess::finished(int) to your slot?
That is one way to do it too, but now I wanted to use the stateChanged signal. It's possible that I'll change to using the finished signal later on if I notice that I don't need to know when it changes to states other than QProcess::NotRunning.

Originally Posted by
ChrisW67
You are saying the actual code posted above, with a minimal main() fails. You don't say what line 18 lists as the source file and line. Is it your code? Has your program been built in debug mode?
What do you mean with line 18? There isn't any line 18 in the code I posted with code on it... Yes, this is my code and I have tried running it in debug mode (see my last post).

Originally Posted by
ChrisW67
The vast majority of segmentation faults are caused by programmers using dangling or uninitialised pointers (or undefined references). There are no pointers in your example code, except for a pointer to p that can only become invalid during program tear-down. It seems likely that the actual code you are running is more complex and is suffering a dangling pointer issue.
I don't think I have any dangling pointer anywhere since I never use the keyword new (or malloc or anything like that), but maybe some pointer is created somewhere that I am not aware of. I had better post the entire code here:
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "process_handler.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
process_handler s;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "process_handler.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
process_handler s;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include <QTimer>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Begin start processes when the windows has finished loading */
QTimer::singleShot(0,
&s,
SLOT(start_process
()));
}
MainWindow::~MainWindow()
{
delete ui;
}
#include <QTimer>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Begin start processes when the windows has finished loading */
QTimer::singleShot(0, &s, SLOT(start_process()));
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
process_handler.h:
#ifndef PROCESS_HANDLER_H
#define PROCESS_HANDLER_H
#include <QProcess>
class process_handler
: public QObject{
Q_OBJECT
public:
process_handler
(QObject *parent
= 0);
private:
public slots:
void start_process();
private slots:
void process_state_changed();
};
#endif // PROCESS_HANDLER_H
#ifndef PROCESS_HANDLER_H
#define PROCESS_HANDLER_H
#include <QProcess>
class process_handler : public QObject
{
Q_OBJECT
public:
process_handler(QObject *parent = 0);
private:
QProcess p;
public slots:
void start_process();
private slots:
void process_state_changed();
};
#endif // PROCESS_HANDLER_H
To copy to clipboard, switch view to plain text mode
process_handler.cpp:
#include <iostream>
using std::cout;
using std::endl;
#include <QTimer>
#include "process_handler.h"
process_handler
::process_handler(QObject *parent
) :{
connect(&p,
SIGNAL(stateChanged
(QProcess::ProcessState)),
this,
SLOT(process_state_changed
()));
}
void process_handler::process_state_changed()
{
cout << endl;
cout << "Process is starting up..." << endl;
}
cout << "Process is now running." << endl;
}
if (p.
state() == QProcess::NotRunning) { cout << "Process is finished running." << endl;
/* Start a new process */
start_process();
//QTimer::singleShot(0, this, SLOT(start_process()));
}
}
void process_handler::start_process()
{
//p.start("program");
p.start("ls");
}
#include <iostream>
using std::cout;
using std::endl;
#include <QTimer>
#include "process_handler.h"
process_handler::process_handler(QObject *parent) :
QObject(parent)
{
connect(&p, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(process_state_changed()));
}
void process_handler::process_state_changed()
{
if (p.state() == QProcess::Starting) {
cout << endl;
cout << "Process is starting up..." << endl;
}
if (p.state() == QProcess::Running) {
cout << "Process is now running." << endl;
}
if (p.state() == QProcess::NotRunning) {
cout << "Process is finished running." << endl;
/* Start a new process */
start_process();
//QTimer::singleShot(0, this, SLOT(start_process()));
}
}
void process_handler::start_process()
{
//p.start("program");
p.start("ls");
}
To copy to clipboard, switch view to plain text mode
As can be seen, main.cpp is unmodified, and only one line each has been added to mainwindow.cpp and two lines to mainwindow.h, dealing with the process_handler object. As I said before, it is possible to make a workaround by using a QTimer::SingleShot (substitude line 27 in process_handler.cpp for line 26), but this is a hack I would definitely prefer not having to use.

Originally Posted by
d_stranz
Thanks! I didn't know about that function, it seems handy.
Bookmarks