http://www.youtube.com/watch?v=yazMHbIew0Q
So my issue is pretty obvious:
- I created a new QT GUI Application
- It is templated as such, that the main thread's constructor creates the UI
- 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:
- 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.
- A thread that communicates with a database, which can make changes to the main UI
The reason why I am using threads
- To learn. Threading seems like it is a crucial skill to understand.
- To make my program more efficient.
- 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"
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread> // Inserted
#include <QDebug> // Inserted
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void THREAD_example
(QThread &cThread
);
// Inserted
public slots:
void METHOD_example(); // Inserted
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread> // Inserted
#include <QDebug> // Inserted
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void THREAD_example(QThread &cThread); // Inserted
public slots:
void METHOD_example(); // Inserted
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// Inserted
void MainWindow
::THREAD_example(QThread &cThread
) {
connect(&cThread,SIGNAL(started()),this,SLOT(METHOD_example()));
}
// Inserted
void MainWindow::METHOD_example()
{
qDebug() << "Snakes";
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// Inserted
void MainWindow::THREAD_example(QThread &cThread)
{
connect(&cThread,SIGNAL(started()),this,SLOT(METHOD_example()));
}
// Inserted
void MainWindow::METHOD_example()
{
qDebug() << "Snakes";
}
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
// Inserted
MainWindow cObject;
cObject.THREAD_example(cThread);
cObject.moveToThread(&cThread);
cThread.start();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
// Inserted
QThread cThread;
MainWindow cObject;
cObject.THREAD_example(cThread);
cObject.moveToThread(&cThread);
cThread.start();
return a.exec();
}
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?
Bookmarks