{
Q_OBJECT
public:
Cycler
(volatile bool &stop_flag
) : QObject(), arbeit
(stop_flag
) {};
virtual ~Cycler(){};
signals:
void done();
public slots:
void cycle() {
arbeit = true;
int i = 0;
while(arbeit) {
Sleep(1000);
std::cout << i++ << " " << std::flush;
};
emit done(); };
private:
volatile bool& arbeit;
};
{
Q_OBJECT
public:
explicit CyclerDialog
(QWidget* parent
= 0, Qt
::WindowFlags f
= 0) : QDialog(parent, f
), cycl
(arbeit
){ 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()), this, SLOT(stop()));
connect(&cycl, SIGNAL(done()), &thrd, SLOT(quit())); };
virtual ~CyclerDialog(){stop(); thrd.wait(1000);};
public slots:
void stop(){arbeit = false;};
private:
volatile bool arbeit;
Cycler cycl;
};
class Cycler : public QObject
{
Q_OBJECT
public:
Cycler(volatile bool &stop_flag) : QObject(), arbeit(stop_flag) {};
virtual ~Cycler(){};
signals:
void done();
public slots:
void cycle() {
arbeit = true;
int i = 0;
while(arbeit) {
Sleep(1000);
std::cout << i++ << " " << std::flush;
};
emit done(); };
private:
volatile bool& arbeit;
};
class CyclerDialog: public QDialog
{
Q_OBJECT
public:
explicit CyclerDialog(QWidget* parent = 0, Qt::WindowFlags f = 0) : QDialog(parent, f), cycl(arbeit){
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()), this, SLOT(stop()));
connect(&cycl, SIGNAL(done()), &thrd, SLOT(quit())); };
virtual ~CyclerDialog(){stop(); thrd.wait(1000);};
public slots:
void stop(){arbeit = false;};
private:
volatile bool arbeit;
Cycler cycl;
QThread thrd;
};
To copy to clipboard, switch view to plain text mode
Bookmarks