allora, dovreoi avercela fatta:
codice:
#ifndef WORKTHREAD_H
#define WORKTHREAD_H
#include <QMutex>
#include <QThread>
#include <QVector>
class WorkerThread : public QThread
{
Q_OBJECT
public:
explicit WorkerThread(QVector<QString> list, int width, int height, QString startDir, QObject *parent = 0, bool b = false);
void run();
bool stop;
private:
QVector<QString> list;
int width;
int height;
QString startDir;
signals:
void valueChanged(QString text);
void finished();
};
#endif // WORKTHREAD_H
#include "workerthread.h"
WorkerThread::WorkerThread(QVector<QString> list, int width, int height, QString startDir, QObject *parent, bool b) :
QThread(parent), stop(b)
{
this->list = list;
this->width = width;
this->height = height;
this->startDir = startDir;
}
void WorkerThread::run()
{
for(int i = 0; i < list.count(); i++)
{
QMutex mutex;
// PREVIENE CHE ALTRI THREAD IMPOSTINO LO STOP
mutex.lock();
if(this->stop) break;
mutex.unlock();
// EMETTE IL SEGNALE PER IMPOSTARE IL TESTO SULLA GUI
emit valueChanged(list.value(i));
this->msleep(500);
}
emit finished();
}
nella finestra:
codice:
#ifndef RESIZEWINDOW_H
#define RESIZEWINDOW_H
#include <QScrollArea>
#include "workerthread.h"
#include <QDebug>
namespace Ui {
class ResizeWindow;
}
class ResizeWindow : public QScrollArea
{
Q_OBJECT
public:
explicit ResizeWindow(QVector<QString> list, int width, int height, QString startDir, QWidget *parent = 0);
~ResizeWindow();
WorkerThread* wt;
private slots:
void on_btnClose_clicked();
public slots:
void onValueChanged(QString text);
void onFinished();
private:
Ui::ResizeWindow *ui;
};
#endif // RESIZEWINDOW_H
#include "resizewindow.h"
#include "ui_resizewindow.h"
ResizeWindow::ResizeWindow(QVector<QString> list, int width, int height, QString startDir, QWidget *parent) :
QScrollArea(parent),
ui(new Ui::ResizeWindow)
{
ui->setupUi(this);
wt = new WorkerThread(list, width, height, startDir, this);
connect(wt, SIGNAL(valueChanged(QString)), this, SLOT(onValueChanged(QString)));
connect(wt, SIGNAL(finished()), this, SLOT(onFinished()));
wt->start();
}
void ResizeWindow::on_btnClose_clicked()
{
wt->stop = true;
this->close();
}
void ResizeWindow::onValueChanged(QString text)
{
ui->txtArea->appendPlainText(text);
}
void ResizeWindow::onFinished()
{
ui->txtArea->appendPlainText("OPERAZONI TERMINATE");
}
ResizeWindow::~ResizeWindow()
{
delete ui;
}
questo funziona.
sono bene accetti eventuali suggerimenti!