tnyblom
24th May 2010, 10:21
Hi,
I've got a GUI application (QApplication) that has a QMainWindow as the GUI. In this I have a button that triggers an action that should be executed in a thread. To achieve this I connect the buttons clicked() signal to a slot in the thread object. However the threads action is executed in the GUI thread.
Problem can be seen with the code below. On (at least) the second click on the button in the GUI I want the GUI to remain responsive and the thread to sleep.
Does anyone have an idea as to what I am doing wrong?
main.cpp
#include <QtGui/QApplication>
#include <QDebug>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
qDebug() << "main:" << a.thread();
return a.exec();
}
thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
class Thread : public QThread
{
Q_OBJECT
public:
explicit Thread(QObject *parent = 0);
void run();
signals:
void WorkDone();
public slots:
void DoWork();
};
#endif // THREAD_H
thread.cpp
#include "thread.h"
#include <QDebug>
Thread::Thread(QObject *parent) :
QThread(parent)
{
qDebug() << "Creating new Thread" << QThread::currentThreadId();
}
void Thread::run()
{
qDebug() << "run:" << QThread::currentThreadId();
exec();
}
void Thread::DoWork()
{
qDebug() << "Thread::DoWork() in thread:" << QThread::currentThreadId();
sleep(10);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "thread.h"
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Thread thread;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QtGui>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
qDebug() << "MainWindow:" << this;
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(widget);
QPushButton *button = new QPushButton("Do work...", widget);
widget->setLayout(layout);
layout->addWidget(button);
setCentralWidget(widget);
thread.start();
connect(button, SIGNAL(clicked()), &thread, SLOT(DoWork()));
}
MainWindow::~MainWindow()
{
thread.exit();
thread.wait();
}
I've got a GUI application (QApplication) that has a QMainWindow as the GUI. In this I have a button that triggers an action that should be executed in a thread. To achieve this I connect the buttons clicked() signal to a slot in the thread object. However the threads action is executed in the GUI thread.
Problem can be seen with the code below. On (at least) the second click on the button in the GUI I want the GUI to remain responsive and the thread to sleep.
Does anyone have an idea as to what I am doing wrong?
main.cpp
#include <QtGui/QApplication>
#include <QDebug>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
qDebug() << "main:" << a.thread();
return a.exec();
}
thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
class Thread : public QThread
{
Q_OBJECT
public:
explicit Thread(QObject *parent = 0);
void run();
signals:
void WorkDone();
public slots:
void DoWork();
};
#endif // THREAD_H
thread.cpp
#include "thread.h"
#include <QDebug>
Thread::Thread(QObject *parent) :
QThread(parent)
{
qDebug() << "Creating new Thread" << QThread::currentThreadId();
}
void Thread::run()
{
qDebug() << "run:" << QThread::currentThreadId();
exec();
}
void Thread::DoWork()
{
qDebug() << "Thread::DoWork() in thread:" << QThread::currentThreadId();
sleep(10);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "thread.h"
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Thread thread;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QtGui>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
qDebug() << "MainWindow:" << this;
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(widget);
QPushButton *button = new QPushButton("Do work...", widget);
widget->setLayout(layout);
layout->addWidget(button);
setCentralWidget(widget);
thread.start();
connect(button, SIGNAL(clicked()), &thread, SLOT(DoWork()));
}
MainWindow::~MainWindow()
{
thread.exit();
thread.wait();
}