ashukla
23rd September 2007, 13:43
Dear Sir!
In this code application is running but give this messge...
Object::connect: No such slot ThreadDialog::closeEvent(event)
/*t.h file*/
#ifndef T_H
#define T_H
#include <QtGui>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent=0);
void setMessage(const QString &message);
void stop();
protected:
void run();
private:
QString msg;
volatile bool stopped;
};
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QApplication *app,QWidget *parent = 0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
MyThread threadA;
MyThread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
#endif
/*t.cpp*/
#include <iostream.h>
#include <t.h>
#include <QString>
int i=0;
MyThread::MyThread(QObject *parent)
: QThread(parent)
{
stopped = false;
}
void MyThread::run()
{
while (!stopped)
{
cerr << qPrintable(msg)<< endl;
//msleep(500);
}
stopped=false;
}
void MyThread::stop()
{
stopped = true;
}
void MyThread::setMessage(const QString &message)
{
msg=message;
//run();
}
ThreadDialog::ThreadDialog(QApplication *app,QWidget *parent)
: QDialog(parent)
{
threadA.setMessage("Anurag");
threadB.setMessage("Sonu");
threadAButton = new QPushButton(tr("Start A"),this);
threadBButton = new QPushButton(tr("Start B"),this);
quitButton = new QPushButton(tr("Quit"),this);
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(threadAButton);
layout->addWidget(threadBButton);
layout->addWidget(quitButton);
QHBoxLayout *mainLayout=new QHBoxLayout;
mainLayout->addLayout(layout);
setLayout(mainLayout);
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
QCloseEvent *event=new QCloseEvent();
connect(quitButton, SIGNAL(clicked()), this, SLOT(closeEvent(event)));
}
void ThreadDialog::startOrStopThreadA()
{
if (threadA.isRunning()) {
threadA.stop();
threadAButton->setText(tr("Start A"));
} else {
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()) {
threadB.stop();
threadBButton->setText(tr("Start B"));
} else {
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
int main(int argc, char **argv)
{
QApplication app(argc,argv);
ThreadDialog t(&app);
t.resize(300,200);
t.show();
return app.exec();
}
plz give some proper suggestions.
with regards!
In this code application is running but give this messge...
Object::connect: No such slot ThreadDialog::closeEvent(event)
/*t.h file*/
#ifndef T_H
#define T_H
#include <QtGui>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent=0);
void setMessage(const QString &message);
void stop();
protected:
void run();
private:
QString msg;
volatile bool stopped;
};
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QApplication *app,QWidget *parent = 0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
MyThread threadA;
MyThread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
#endif
/*t.cpp*/
#include <iostream.h>
#include <t.h>
#include <QString>
int i=0;
MyThread::MyThread(QObject *parent)
: QThread(parent)
{
stopped = false;
}
void MyThread::run()
{
while (!stopped)
{
cerr << qPrintable(msg)<< endl;
//msleep(500);
}
stopped=false;
}
void MyThread::stop()
{
stopped = true;
}
void MyThread::setMessage(const QString &message)
{
msg=message;
//run();
}
ThreadDialog::ThreadDialog(QApplication *app,QWidget *parent)
: QDialog(parent)
{
threadA.setMessage("Anurag");
threadB.setMessage("Sonu");
threadAButton = new QPushButton(tr("Start A"),this);
threadBButton = new QPushButton(tr("Start B"),this);
quitButton = new QPushButton(tr("Quit"),this);
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(threadAButton);
layout->addWidget(threadBButton);
layout->addWidget(quitButton);
QHBoxLayout *mainLayout=new QHBoxLayout;
mainLayout->addLayout(layout);
setLayout(mainLayout);
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
QCloseEvent *event=new QCloseEvent();
connect(quitButton, SIGNAL(clicked()), this, SLOT(closeEvent(event)));
}
void ThreadDialog::startOrStopThreadA()
{
if (threadA.isRunning()) {
threadA.stop();
threadAButton->setText(tr("Start A"));
} else {
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()) {
threadB.stop();
threadBButton->setText(tr("Start B"));
} else {
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
int main(int argc, char **argv)
{
QApplication app(argc,argv);
ThreadDialog t(&app);
t.resize(300,200);
t.show();
return app.exec();
}
plz give some proper suggestions.
with regards!