You don't have to subclass QThread, you can simply use existing classes for compilation and use them "directly":
#include <QtGui>
{
Q_OBJECT
public Q_SLOTS:
void doCalc()
{
for (int i = 1; i < 25; ++i)
{
Q_EMIT message
(QString::number(i
));
t = t.addSecs(2);
while (t >
QTime::currentTime()) {} }
}
Q_SIGNALS:
};
int main(int argv, char** args)
{
// GUI
la->addWidget(l);
la->addWidget(b);
la->addWidget(le);
w.setLayout(la);
w.show();
// Thread
calc c;
c.moveToThread(&t);
t.start();
// Connections
QObject::connect(b,
SIGNAL(clicked
()),
&c,
SLOT(doCalc
()));
return app.exec();
}
#include "main.moc"
#include <QtGui>
class calc : public QObject
{
Q_OBJECT
public Q_SLOTS:
void doCalc()
{
for (int i = 1; i < 25; ++i)
{
Q_EMIT message(QString::number(i));
QTime t = QTime::currentTime();
t = t.addSecs(2);
while (t > QTime::currentTime()) {}
}
}
Q_SIGNALS:
void message(QString);
};
int main(int argv, char** args)
{
QApplication app(argv, args);
// GUI
QWidget w;
QPushButton* b = new QPushButton("start", &w);
QLabel* l = new QLabel(&w);
QLineEdit* le = new QLineEdit(&w);
QVBoxLayout* la = new QVBoxLayout();
la->addWidget(l);
la->addWidget(b);
la->addWidget(le);
w.setLayout(la);
w.show();
// Thread
calc c;
QThread t;
c.moveToThread(&t);
t.start();
// Connections
QObject::connect(&c, SIGNAL(message(QString)), l, SLOT(setText(QString)));
QObject::connect(b, SIGNAL(clicked()), &c, SLOT(doCalc()));
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks