Results 1 to 14 of 14

Thread: QThread slot executed in GUI thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread slot executed in GUI thread

    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
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QDebug>
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. qDebug() << "main:" << a.thread();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    thread.h
    Qt Code:
    1. #ifndef THREAD_H
    2. #define THREAD_H
    3.  
    4. #include <QThread>
    5.  
    6. class Thread : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit Thread(QObject *parent = 0);
    11. void run();
    12.  
    13. signals:
    14. void WorkDone();
    15.  
    16. public slots:
    17. void DoWork();
    18. };
    19.  
    20. #endif // THREAD_H
    To copy to clipboard, switch view to plain text mode 

    thread.cpp
    Qt Code:
    1. #include "thread.h"
    2.  
    3. #include <QDebug>
    4.  
    5. Thread::Thread(QObject *parent) :
    6. QThread(parent)
    7. {
    8. qDebug() << "Creating new Thread" << QThread::currentThreadId();
    9. }
    10.  
    11. void Thread::run()
    12. {
    13. qDebug() << "run:" << QThread::currentThreadId();
    14. exec();
    15. }
    16.  
    17. void Thread::DoWork()
    18. {
    19. qDebug() << "Thread::DoWork() in thread:" << QThread::currentThreadId();
    20. sleep(10);
    21. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include "thread.h"
    5.  
    6. #include <QMainWindow>
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15.  
    16. private:
    17. Thread thread;
    18. };
    19.  
    20. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QtGui>
    4. #include <QDebug>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent)
    8. {
    9. qDebug() << "MainWindow:" << this;
    10. QWidget *widget = new QWidget();
    11. QHBoxLayout *layout = new QHBoxLayout(widget);
    12. QPushButton *button = new QPushButton("Do work...", widget);
    13. widget->setLayout(layout);
    14. layout->addWidget(button);
    15. setCentralWidget(widget);
    16. thread.start();
    17. connect(button, SIGNAL(clicked()), &thread, SLOT(DoWork()));
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. thread.exit();
    23. thread.wait();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tnyblom; 24th May 2010 at 10:26.

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 03:27
  2. Replies: 9
    Last Post: 28th November 2009, 20:31
  3. Replies: 1
    Last Post: 11th September 2008, 20:45
  4. Replies: 4
    Last Post: 26th June 2008, 18:41
  5. QThread, QMessageBox, gui thread
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2006, 12:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.