My mainwindow have a side GUI with a QGraphicsView in the center, there is a logic class which make different calculations which triggered by the GUI and effects the QGraphicsView. Some of the calculations are heavy which make the GUI go to sleep, there is a QProgressBar and some other Qt items which provides some data while the calculations are been made, so when the GUI process goes to sleep those items show there updated result only when the process has finished. I understand that it because the logic class and the UI are under the same process,
I was trying to correct this by doing: Maya’s Programming Blog [mayaposch.wordpress.com]
But I came to understanding that this method is not sufficient to my code as I have several methods that run heavy calculations and some of them returning values. Maya’s method talking about doing something like this:
connect(thread, SIGNAL(started()), worker, SLOT(process()));...thread->start();
, but in my code there is no single main process but many, so if I want to work in that way, from what I understand I need to create a thread before each process method, move the logic class to this thread and than connect the process method with the thread start method, which not sounds to me as the proper way to do this.
So I ask for a way to fully separate the logic layer from the view layer, so any method which invoked the logic layer will run on a different process (which is the same for all logic class methods), so the view layer won’t go to sleep.
Note: there is no problems of synchronicity, when a calculation been made it’s the only one which work at a time.
An example of my problem:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "Logic/worker.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void startProcessing1();
private slots:
void on_pushButton_1_clicked();
void on_pushButton_2_clicked();
void processing1Done();
private:
Ui::MainWindow *ui;
Worker* m_worker;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QThread"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->progressBar_1->setVisible(false);
ui->progressBar_2->setVisible(false);
QThread* thread = new QThread;
m_worker = new Worker();
m_worker->moveToThread(thread);
connect(this, SIGNAL(startProcessing1()), m_worker, SLOT(process1()));
connect(m_worker, SIGNAL(finished1()), this, SLOT(processing1Done()));
connect(m_worker, SIGNAL(changePbar1(int)), ui->progressBar_1, SLOT(setValue(int)));
connect(m_worker, SIGNAL(finished1()), thread, SLOT(quit()));
connect(m_worker, SIGNAL(finished1()), m_worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
MainWindow::~MainWindow()
{
delete m_worker;
delete ui;
}
void MainWindow::on_pushButton_1_clicked()
{
ui->progressBar_1->setVisible(true);
emit startProcessing1();
}
void MainWindow::on_pushButton_2_clicked()
{
}
void MainWindow::processing1Done()
{
ui->progressBar_1->setVisible(false);
}
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
signals:
void finished1();
void changePbar1(int val);
void finished2();
public slots:
void process1();
void process2();
};
#endif // WORKER_H
#include "worker.h"
Worker::Worker(QObject *parent) :
QObject(parent)
{
}
void Worker::process1()
{
for(int i = 0; i < 100000; ++i)
{
qDebug("Hello World!");
emit changePbar1(i);
}
emit finished1();
}
void Worker::process2()
{
for(int i = 0; i < 100000; ++i)
{
qDebug("Hello World!");
}
emit finished2();
}
↧