PDA

View Full Version : Object::connect: No such slot ThreadDialog::closeEvent(event)



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!

jpn
23rd September 2007, 14:51
As it says, QWidget::closeEvent() is not a slot. Furthermore, signals and slots don't work that way. You can't add variables to connect statement.

connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
Recommended reading: signals and slots (http://doc.trolltech.com/latest/signalsandslots.html)

ashukla
24th September 2007, 05:38
As it says, QWidget::closeEvent() is not a slot. Furthermore, signals and slots don't work that way. You can't add variables to connect statement.

connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
Recommended reading: signals and slots (http://doc.trolltech.com/latest/signalsandslots.html)
Dear Sir!
I have used close slot before viewing this post. But I couldn't think this QWidget::closeEvent() is a function however I have seen this function earlier; So I am facing trouble.
Thanks!

jpn
24th September 2007, 08:20
Don't guess and seem to remember which one is slot and which one is not. It is all clearly and well documented in the Qt reference documentation. I keep Qt Assistant always open while writing anything related to Qt. I suggest you do the same. ;)