PDA

View Full Version : Object::connect: No such signal QProces?????



nthung
5th October 2011, 18:12
header file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>
#include <QPushButton>
#include <qmessagebox.h>
#include <qdebug.h>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;
QProcess *pr;
signals:
void StartProcess(bool timerout);
public slots:
void SaveLog(bool timerout);
private:

};

#endif // MAINWINDOW_H

source file

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pr=new QProcess();
connect(pr,SIGNAL(StartProcess(bool)),this, SLOT(SaveLog(bool )));
}

MainWindow::~MainWindow()
{
delete ui;
}

//void MainWindow::StartProcess(){}
void MainWindow::SaveLog(bool timerout){
emit StartProcess(timerout);
pr->start("E:\\test\\ttttt.exe");

if(pr->waitForFinished(3000)==false){
QMessageBox::warning(
this,
tr("timeout"),
tr("timeout") );


}


}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

but when i run code
it displayes the erors

Starting C:\Users\nthung\Documents\test1\debug\test1.exe...
Object::connect: No such signal QProcess::StartProcess(bool) in mainwindow.cpp:10
Object::connect: (receiver name: 'MainWindow')
C:\Users\nthung\Documents\test1\debug\test1.exe exited with code 0
Help me?
Thanks

Lykurg
5th October 2011, 22:32
It is a signal of your class and not QProcess! So use "this, SIGNAL(...)".

stampede
5th October 2011, 22:45
You are doing something weird, connecting a slot to a signal, which is emitted from this slot ... I don't really understand what is the purpose.
What are you trying to achieve ?

ChrisW67
5th October 2011, 23:10
See Time out for a QProcess to see the origin of this odyssey.

If you want to start a program, terminate it after 3 seconds if it has not finished already, and do not mind the whole application being unresponsive during that time then it need be no more complicated than this (Nish said this in the other thread):


// An example, long running program on my machine
QString cmd = "/bin/ls";
QStringList args = QStringList() << "-lR" << "/";

const int timeoutPeriod = 3000;

QProcess proc;
proc.start(cmd, args);
bool finished = proc.waitForFinished(timeoutPeriod);
if (!finished)
proc.terminate();


If you want your application to remain responsive, perhaps reading the output of the sub program, during the time out period than you need to be more involved.

Which is it?

nthung
6th October 2011, 02:31
You are doing something weird, connecting a slot to a signal, which is emitted from this slot ... I don't really understand what is the purpose.
What are you trying to achieve ?
I want to start a program, terminate it after 3 seconds if it has not finished already,
Thanks

ChrisW67
6th October 2011, 06:12
Do you need your program to be responsive while that three seconds is expiring?

nthung
6th October 2011, 07:50
Do you need your program to be responsive while that three seconds is expiring?
yes
Thanks

stampede
6th October 2011, 09:02
I want to start a program, terminate it after 3 seconds if it has not finished already,
Should be simple - connect QProcess::started() signal to a 3 sec. timer's start() slot. Connect the timer's timeout() signal to your slot, where you will check if the process is finished - if not, kill it.
Don't use QProcess::waitForFinished, because it will freeze your gui - you want to remain responsive.

nthung
6th October 2011, 10:14
Should be simple - connect QProcess::started() signal to a 3 sec. timer's start() slot. Connect the timer's timeout() signal to your slot, where you will check if the process is finished - if not, kill it.
Don't use QProcess::waitForFinished, because it will freeze your gui - you want to remain responsive.
I will code as your guide
But I wonder that how to know about a Process is timeout? because that it has no timeout function
So I Coded as

#ifndef QPROCESSTIMEOUT_H
#define QPROCESSTIMEOUT_H
#include<QProcess>
#include<QDebug>
#include<QTimer>
class QProcessTimeout:public QObject
{
Q_OBJECT
public:
QProcessTimeout();
void Timeout(int seconds){
pr->start("C:\\Tesst.exe");
emit TimeoutSignal(seconds);
}
signals:
void TimeoutSignal(int seconds);
public slots:
void TimeOut(int x){
if(pr->waitForFinished(x)==false){
qDebug()<<"Time out";
pr->kill();
}
else
qDebug()<<"Sucessfully";
}
public:
QProcess*pr;
QTimer timer;
};

#endif // QPROCESSTIMEOUT_H


#include "qprocesstimeout.h"

QProcessTimeout::QProcessTimeout()
{
pr=new QProcess();
}


#include <QtCore/QCoreApplication>
#include<qprocesstimeout.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcessTimeout x;
QObject::connect(&x,SIGNAL(TimeoutSignal(int)),&x,SLOT(TimeOut(int)));
x.Timeout(3999);
return a.exec();
}


Thanks for supporting

ChrisW67
6th October 2011, 23:43
You want your program to be responsive during the time out period. You cannot use QProcess::waitForFinished() because it:


Blocks until the process has finished and the finished() signal has been emitted, or until msecs milliseconds have passed.

Here is a version of what Stampede suggested:


#include <QtGui>
#include <QDebug>

class Launcher: public QObject {
Q_OBJECT
static const int timeoutPeriod = 3000;
QTimer m_timer;
QProcess m_proc;

public:
Launcher(QObject *p = 0): QObject(p) {
// Set up the timer
m_timer.setInterval( timeoutPeriod );
m_timer.setSingleShot( true );

// These connections do the work
connect(&m_proc, SIGNAL(started()), &m_timer, SLOT(start()));
connect(&m_timer, SIGNAL(timeout()), this, SLOT(timedOut()));

// these are here only to illustrate what is being called
connect(&m_proc, SIGNAL(started()), SLOT(processStarted()));
connect(&m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(processFinished(int,QProcess::ExitStatus)));
}

void launch() {
static const QString cmd = "/bin/ls";
static const QStringList args = QStringList() << "-lR" << "/";

// Start the process
m_proc.start(cmd, args);
}

public slots:
void timedOut() {
qDebug() << "Timed out";
if (m_proc.state() != QProcess::NotRunning) {
qDebug() << "Terminating";
m_proc.terminate();
}
}

// these are here only to illustrate what is being called
void processStarted() { qDebug() << "Process started"; }
void processFinished(int exitCode, QProcess::ExitStatus exitStatus) { qDebug() << "Process Finished" << exitCode << exitStatus; }
};

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Launcher launcher;
launcher.launch();
return app.exec();
}
#include "main.moc"

You could improve it by stopping the timer when the process finishes.

nthung
7th October 2011, 03:17
You want your program to be responsive during the time out period. You cannot use QProcess::waitForFinished() because it:

Here is a version of what Stampede suggested:


connect(&m_proc, SIGNAL(started()), SLOT(processStarted()));

You could improve it by stopping the timer when the process finishes.
thanks so much for supporting.
your code is esaly understandable.
but the line code above lost a paramater, it is timer?

ChrisW67
7th October 2011, 04:53
No, it has not lost a parameter, and no it is not the timer: try it and see. Have a look at QObject::connect()... hint; there's more than one.