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?