class buttonThread
: public QThread{
Q_OBJECT
public:
buttonThread
(QWidget *parent
= 0) {};
void run();
};
{
Q_OBJECT
public:
signals:
void buttonHasBeenClicked(buttonThread *, int);
public slots:
void threadClicked();
};
{
Q_OBJECT
public:
public slots:
void buttonClicked(buttonThread *current, int);
private:
QVector<QPushButton *> pushb;
QVector<buttonThread *> threads;
QVector<message *> msg;
};
#define NBTHREADS 2
int val = 0;
{
printf("buttons(): QThread::currentThread() = %p\n",
QThread::currentThread());
for (int i=0 ; i<NBTHREADS ; i++) {
// Button
layout->addWidget(this->pushb[i]);
this->msg << new message(this);
QObject::connect(this
->pushb
[i
],
SIGNAL(clicked
()), this
->msg
[i
],
SLOT(threadClicked
()));
QObject::connect(this
->msg
[i
],
SIGNAL(buttonHasBeenClicked
(buttonThread
*,
int)),
this,
SLOT(buttonClicked
(buttonThread
*,
int)));
// Thread
this->threads << new buttonThread(this);
this->msg[i]->moveToThread(this->threads[i]);
// Start thread
this->threads[i]->start();
}
}
void
buttons::buttonClicked(buttonThread *current, int value)
{
int id = this->threads.indexOf(current);
if (id == -1) {
msg->exec();
return;
}
msg->exec();
}
void
message::threadClicked()
{
printf("threadClicked(): QThread::currentThread() = %p\n",
QThread::currentThread());
emit buttonHasBeenClicked
((buttonThread
*)QThread::currentThread(), val
++);
}
void
buttonThread::run()
{
printf("run(): QThread::currentThread() = %p\n",
QThread::currentThread());
this->exec();
}
class buttonThread : public QThread
{
Q_OBJECT
public:
buttonThread(QWidget *parent = 0) {};
void run();
};
class message : public QObject
{
Q_OBJECT
public:
message(QWidget *parent = 0) {};
signals:
void buttonHasBeenClicked(buttonThread *, int);
public slots:
void threadClicked();
};
class buttons : public QWidget
{
Q_OBJECT
public:
buttons(QWidget *parent = 0);
public slots:
void buttonClicked(buttonThread *current, int);
private:
QVector<QPushButton *> pushb;
QVector<buttonThread *> threads;
QVector<message *> msg;
};
#define NBTHREADS 2
int val = 0;
buttons::buttons(QWidget *parent)
{
printf("buttons(): QThread::currentThread() = %p\n", QThread::currentThread());
QHBoxLayout *layout = new QHBoxLayout(this);
for (int i=0 ; i<NBTHREADS ; i++) {
// Button
this->pushb << new QPushButton(QString("Thread %1").arg(i), this);
layout->addWidget(this->pushb[i]);
this->msg << new message(this);
QObject::connect(this->pushb[i], SIGNAL(clicked()), this->msg[i], SLOT(threadClicked()));
QObject::connect(this->msg[i], SIGNAL(buttonHasBeenClicked(buttonThread *, int)), this, SLOT(buttonClicked(buttonThread *, int)));
// Thread
this->threads << new buttonThread(this);
this->msg[i]->moveToThread(this->threads[i]);
// Start thread
this->threads[i]->start();
}
}
void
buttons::buttonClicked(buttonThread *current, int value)
{
int id = this->threads.indexOf(current);
if (id == -1) {
QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString("Error"), QString("buttons::buttonClicked: current=%1 not found in thread list").arg((long)current, 0, 16), QMessageBox::Ok);
msg->exec();
return;
}
QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString("Button clicked"), QString("button %1 clicked - val = %2").arg(id).arg(value), QMessageBox::Ok);
msg->exec();
}
void
message::threadClicked()
{
printf("threadClicked(): QThread::currentThread() = %p\n", QThread::currentThread());
emit buttonHasBeenClicked((buttonThread *)QThread::currentThread(), val++);
}
void
buttonThread::run()
{
printf("run(): QThread::currentThread() = %p\n", QThread::currentThread());
this->exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks