#include <QtGui>
#include <QApplication>
class buttonThread
: public QThread{
Q_OBJECT
public:
buttonThread
(QWidget *parent
= 0,
int number
= 0);
public slots:
void threadClicked();
private:
static int val;
QVector<long> thread_ids;
int GetId();
};
int buttonThread::val = 0;
{
Q_OBJECT
public:
private:
buttonThread *threads[2];
};
{
layout->addWidget(buttons[0]);
threads[0] = new buttonThread(this, 1);
QObject::connect(this
->buttons
[0],
SIGNAL(clicked
()), threads
[0],
SLOT(threadClicked
()));
layout->addWidget(buttons[1]);
threads[1] = new buttonThread(this, 2);
QObject::connect(this
->buttons
[1],
SIGNAL(clicked
()), threads
[1],
SLOT(threadClicked
()));
threads[0]->start();
threads[1]->start();
}
buttonThread
::buttonThread(QWidget *parent,
int number
){
this->thread_ids.resize(2);
this
->thread_ids
[number
-1] = (long)QThread::currentThread();
}
int
buttonThread::GetId()
{
return this
->thread_ids.
indexOf((long)QThread::currentThread()) + 1;
}
void
buttonThread::threadClicked()
{
val++;
msg->exec();
}
int main(int argc, char *argv[])
{
boutons w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QApplication>
class buttonThread : public QThread
{
Q_OBJECT
public:
buttonThread(QWidget *parent = 0, int number = 0);
public slots:
void threadClicked();
private:
QMessageBox *msg;
static int val;
QVector<long> thread_ids;
int GetId();
};
int buttonThread::val = 0;
class boutons : public QWidget
{
Q_OBJECT
public:
boutons(QWidget *parent = 0);
private:
QPushButton *buttons[2];
buttonThread *threads[2];
};
boutons::boutons(QWidget *parent)
{
QHBoxLayout *layout = new QHBoxLayout(this);
buttons[0] = new QPushButton("Thread 1", this);
layout->addWidget(buttons[0]);
threads[0] = new buttonThread(this, 1);
QObject::connect(this->buttons[0], SIGNAL(clicked()), threads[0], SLOT(threadClicked()));
buttons[1] = new QPushButton("Thread 2", this);
layout->addWidget(buttons[1]);
threads[1] = new buttonThread(this, 2);
QObject::connect(this->buttons[1], SIGNAL(clicked()), threads[1], SLOT(threadClicked()));
threads[0]->start();
threads[1]->start();
}
buttonThread::buttonThread(QWidget *parent, int number)
{
this->thread_ids.resize(2);
this->thread_ids[number-1] = (long)QThread::currentThread();
}
int
buttonThread::GetId()
{
return this->thread_ids.indexOf((long)QThread::currentThread()) + 1;
}
void
buttonThread::threadClicked()
{
QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString(), QString("Thread %1 : val = %2").arg(this->GetId()).arg(val), QMessageBox::Ok);
val++;
msg->exec();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
boutons w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks