
Originally Posted by
Tio
Can you gimme a little example using exec() and quit()?
background.h:
#include <QList>
#include <QThread>
class Background
: public QThread { Q_OBJECT;
QList<int> prime;
public:
Background();
void run();
signals:
void proceed();
private slots:
void getNextPrime();
};
#include <QList>
#include <QThread>
class Background : public QThread {
Q_OBJECT;
QList<int> prime;
public:
Background();
void run();
signals:
void gotNextPrime(QString);
void proceed();
private slots:
void getNextPrime();
};
To copy to clipboard, switch view to plain text mode
background.cpp:
#include "background.h"
Background::Background() {
moveToThread(this);
connect(this, SIGNAL(proceed()), SLOT(getNextPrime()), Qt::QueuedConnection);
prime += 2;
}
void Background::run() {
emit proceed();
exec();
}
void Background::getNextPrime() {
for (int p = prime.last() + 1;; ++p) for (int i = 0;; ++i) {
int j = prime.at(i);
int k = p / j;
if (p == j * k) break;
if (j < k) continue;
prime += p;
emit gotNextPrime
(QString("Prime %1 is %2.").
arg(prime.
count()).
arg(p
));
emit proceed();
return;
}
}
#include "background.h"
Background::Background() {
moveToThread(this);
connect(this, SIGNAL(proceed()), SLOT(getNextPrime()), Qt::QueuedConnection);
prime += 2;
}
void Background::run() {
emit proceed();
exec();
}
void Background::getNextPrime() {
for (int p = prime.last() + 1;; ++p) for (int i = 0;; ++i) {
int j = prime.at(i);
int k = p / j;
if (p == j * k) break;
if (j < k) continue;
prime += p;
emit gotNextPrime(QString("Prime %1 is %2.").arg(prime.count()).arg(p));
emit proceed();
return;
}
}
To copy to clipboard, switch view to plain text mode
mainwindow.h:
#include <QGridLayout>
#include <QLabel>
#include <QMainWindow>
#include <QPushButton>
#include <QWidget>
#include "background.h"
Q_OBJECT
public:
private:
Background *bgnd;
};
#include <QGridLayout>
#include <QLabel>
#include <QMainWindow>
#include <QPushButton>
#include <QWidget>
#include "background.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private:
QGridLayout *grid;
QWidget *centralWidget;
QLabel *displayLabel;
QPushButton *stopButton;
QPushButton *startButton;
Background *bgnd;
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
displayLabel
= new QLabel(centralWidget
);
displayLabel->setAlignment(Qt::AlignCenter);
displayLabel->setText("Click Start to begin.");
startButton->setText("Start");
stopButton->setText("Stop");
grid->addWidget(displayLabel, 0, 0, 1, 2);
grid->addWidget(startButton, 1, 0);
grid->addWidget(stopButton, 1, 1);
setCentralWidget(centralWidget);
bgnd = new Background();
connect (startButton, SIGNAL(clicked()), bgnd, SLOT(start()), Qt::DirectConnection);
connect (stopButton, SIGNAL(clicked()), bgnd, SLOT(quit()));
connect (bgnd,
SIGNAL(gotNextPrime
(QString)), displayLabel,
SLOT(setText
(QString)));
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
centralWidget = new QWidget(this);
displayLabel = new QLabel(centralWidget);
displayLabel->setAlignment(Qt::AlignCenter);
displayLabel->setText("Click Start to begin.");
startButton = new QPushButton(centralWidget);
startButton->setText("Start");
stopButton = new QPushButton(centralWidget);
stopButton->setText("Stop");
grid = new QGridLayout(centralWidget);
grid->addWidget(displayLabel, 0, 0, 1, 2);
grid->addWidget(startButton, 1, 0);
grid->addWidget(stopButton, 1, 1);
setCentralWidget(centralWidget);
bgnd = new Background();
connect (startButton, SIGNAL(clicked()), bgnd, SLOT(start()), Qt::DirectConnection);
connect (stopButton, SIGNAL(clicked()), bgnd, SLOT(quit()));
connect (bgnd, SIGNAL(gotNextPrime(QString)), displayLabel, SLOT(setText(QString)));
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[]) {
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Note that the connect in the Background constructor must specify Qt::QueuedConnection — otherwise, since the signal and slot are on the same thread, the emit proceed(); would become effectively a recursive call to getNextPrime(); we need it to go through the queue so a quit() can interrupt the sequence.
The connect for StartButton has to specify Qt::DirectConnection because until bgnd is started, there is no event loop to process a queued connection.
Bookmarks