Results 1 to 6 of 6

Thread: Help with VoidRealms "Threading done correctly"

  1. #1
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Help with VoidRealms "Threading done correctly"

    http://www.youtube.com/watch?v=yazMHbIew0Q


    So my issue is pretty obvious:
    1. I created a new QT GUI Application
    2. It is templated as such, that the main thread's constructor creates the UI
    3. Thus, when I try to thread as Void Realms does, it creates a duplicate instance of the ui.


    This is what I am trying to accomplish in a larger program:
    1. A properly threaded program; I understand that the Qt documentation has been troublesome in this area. I am unsure if this is still the case.
    2. A thread that communicates with a database, which can make changes to the main UI


    The reason why I am using threads
    1. To learn. Threading seems like it is a crucial skill to understand.
    2. To make my program more efficient.
    3. To avoid having my UI hang while it is trying to communicate with the database [which is over a network].


    I went ahead and created a new application in QTCreator, File>New File or Project>Applications>QTGui Application
    The lines I added from following Void Realms will have a comment beside them, "// Inserted"

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QThread> // Inserted
    6. #include <QDebug> // Inserted
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19. void THREAD_example(QThread &cThread); // Inserted
    20.  
    21. public slots:
    22. void METHOD_example(); // Inserted
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. // Inserted
    17. void MainWindow::THREAD_example(QThread &cThread)
    18. {
    19. connect(&cThread,SIGNAL(started()),this,SLOT(METHOD_example()));
    20. }
    21.  
    22. // Inserted
    23. void MainWindow::METHOD_example()
    24. {
    25. qDebug() << "Snakes";
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. // Inserted
    11. QThread cThread;
    12. MainWindow cObject;
    13. cObject.THREAD_example(cThread);
    14. cObject.moveToThread(&cThread);
    15. cThread.start();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 


    So how should I change this code, that while still threading "correctly", that I am not creating a duplicate instance of the UI?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with VoidRealms "Threading done correctly"

    You cannot access the user interface from a worker thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Help with VoidRealms "Threading done correctly"

    Right, I remember that; Give me a half an hour, and I'll try to restructure the application above that the main is setup to receive a signal from the worker.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with VoidRealms "Threading done correctly"

    Just remember that using threads "just because you can" is usually a bad idea. Think if you really benefit from having threads in your program in any way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Help with VoidRealms "Threading done correctly"

    I believe I figured out what I needed to do.

    I made a new class, and just added a parameter to my connections which takes the ui object created in main.cpp

    Without going too much into it, here is a snippet from my worker class source
    Qt Code:
    1. void example::THREAD_example(QThread &cThread, MainWindow &w)
    2. {
    3. connect(&cThread,SIGNAL(started()),&w,SLOT(METHOD_example()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    METHOD_example() is a public slot in the main thread, which takes signals from my worker class, blah blah blah

    anyways, thanks :P

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with VoidRealms "Threading done correctly"

    Your original code was almost correct. You can make it work properly by just removing this line
    Qt Code:
    1. cObject.moveToThread(&cThread);
    To copy to clipboard, switch view to plain text mode 

    But since the thread is not doing anything it is not very useful

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 3rd December 2013, 02:19
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Replies: 3
    Last Post: 15th February 2010, 17:27
  4. Replies: 0
    Last Post: 15th November 2009, 09:40
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.