Onanymous
19th July 2011, 08:41
I was intrigued by the post http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ and really like that idea of new way of using threads. So I wrote a simple example, which... does not work
#ifndef THREADs_TEST_H
#define THREADs_TEST_H
#if defined(_WIN32)
#include <windows.h>
#elif defined(__GNUC__)
#include <unistd.h>
#define Sleep(x) sleep((x)/1000)
#endif //_WIN32
#include <iostream>
#include <QThread>
#include <QObject>
#include <QDialog>
#include <QDialogButtonBox>
#include <QBoxLayout>
#include <QPushButton>
class Cycler : public QObject
{
Q_OBJECT
public:
Cycler() : QObject() {};
virtual ~Cycler(){stop();};
signals:
void done();
public slots:
void cycle() {
arbeit = true;
int i = 0;
while(arbeit) {
Sleep(1000);
std::cout << i++ << " " << std::flush;
};
emit done(); };
void stop(){ arbeit = false;};
private:
volatile bool arbeit;
};
class CyclerDialog: public QDialog
{
Q_OBJECT
public:
explicit CyclerDialog(QWidget* parent = 0, Qt::WindowFlags f = 0) : QDialog(parent, f){
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QVBoxLayout *main_layout = new QVBoxLayout;
main_layout->addWidget(buttons);
setLayout(main_layout);
cycl.moveToThread(&thrd);
connect(buttons, SIGNAL(accepted()), &thrd, SLOT(start()));
connect(&thrd, SIGNAL(started()), &cycl, SLOT(cycle()));
connect(buttons, SIGNAL(rejected()), &cycl, SLOT(stop()));
connect(&cycl, SIGNAL(done()), &thrd, SLOT(quit())); };
virtual ~CyclerDialog(){thrd.wait(1000);};
private:
Cycler cycl;
QThread thrd;
};
#endif // THREADs_TEST_H
#include <QApplication>
#include "threads_test.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CyclerDialog window;
window.show();
return(app.exec());
}
It is supposed to start printing numbers in std::cout when OK is clicked, and stop when CANCEL is clicked, but apparently stop() slot is never called. WHY????
#ifndef THREADs_TEST_H
#define THREADs_TEST_H
#if defined(_WIN32)
#include <windows.h>
#elif defined(__GNUC__)
#include <unistd.h>
#define Sleep(x) sleep((x)/1000)
#endif //_WIN32
#include <iostream>
#include <QThread>
#include <QObject>
#include <QDialog>
#include <QDialogButtonBox>
#include <QBoxLayout>
#include <QPushButton>
class Cycler : public QObject
{
Q_OBJECT
public:
Cycler() : QObject() {};
virtual ~Cycler(){stop();};
signals:
void done();
public slots:
void cycle() {
arbeit = true;
int i = 0;
while(arbeit) {
Sleep(1000);
std::cout << i++ << " " << std::flush;
};
emit done(); };
void stop(){ arbeit = false;};
private:
volatile bool arbeit;
};
class CyclerDialog: public QDialog
{
Q_OBJECT
public:
explicit CyclerDialog(QWidget* parent = 0, Qt::WindowFlags f = 0) : QDialog(parent, f){
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QVBoxLayout *main_layout = new QVBoxLayout;
main_layout->addWidget(buttons);
setLayout(main_layout);
cycl.moveToThread(&thrd);
connect(buttons, SIGNAL(accepted()), &thrd, SLOT(start()));
connect(&thrd, SIGNAL(started()), &cycl, SLOT(cycle()));
connect(buttons, SIGNAL(rejected()), &cycl, SLOT(stop()));
connect(&cycl, SIGNAL(done()), &thrd, SLOT(quit())); };
virtual ~CyclerDialog(){thrd.wait(1000);};
private:
Cycler cycl;
QThread thrd;
};
#endif // THREADs_TEST_H
#include <QApplication>
#include "threads_test.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CyclerDialog window;
window.show();
return(app.exec());
}
It is supposed to start printing numbers in std::cout when OK is clicked, and stop when CANCEL is clicked, but apparently stop() slot is never called. WHY????