Hi, anda_skoa.
First of all, thank you for answering me.
Before reading your considerations, I was modifying the project to simplify it and thus better understand how to work with threads and why it is giving error.
You could take a look to see where I'm going wrong, the project was like this.
Thank you for your attention.
// Arquivo .h
#include <QObject>
#include <QThread>
{
Q_OBJECT
public:
clsThrTask
(QString pstrIdThread,
QObject *parent
= nullptr
);
// Constructor with paramenter void run() override;
signals:
// Arquivo .h
#include <QObject>
#include <QThread>
class clsThrTask : public QThread
{
Q_OBJECT
public:
clsThrTask(QString pstrIdThread, QObject *parent = nullptr); // Constructor with paramenter
void run() override;
signals:
void sigRetorno(QString);
To copy to clipboard, switch view to plain text mode
//Arquivo .cpp
#include "clsthrtask.h"
{
vstrForRef = pstrIdThread;
}
void clsThrTask::run()
{
QString vstrResposta
= "Threads nº: " + QString::number((long long) QThread::currentThreadId(),
16) + " | FOR REF nº: " + vstrForRef;
// EMITE UM SIGNAL WITH DATA
emit sigRetorno(vstrResposta);
}
//Arquivo .cpp
#include "clsthrtask.h"
clsThrTask::clsThrTask(QString pstrIdThread, QObject *parent) : QThread(parent)
{
vstrForRef = pstrIdThread;
}
void clsThrTask::run()
{
QString vstrResposta = "Threads nº: " + QString::number((long long) QThread::currentThreadId(),16) + " | FOR REF nº: " + vstrForRef;
// EMITE UM SIGNAL WITH DATA
emit sigRetorno(vstrResposta);
}
To copy to clipboard, switch view to plain text mode
//Arquivo MainWindow.h
#include "clsthrtask.h"
namespace Ui {
class MainWindow;
}
class clsThrTask;
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= nullptr
);
~MainWindow();
//Arquivo MainWindow.h
#include "clsthrtask.h"
namespace Ui {
class MainWindow;
}
class clsThrTask;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
To copy to clipboard, switch view to plain text mode
//Arquivo MainWindow.cpp
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
// ARRAY OF THREADS
for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
{
vthrThread
[vnumIni
] = new QThread(QString::number(vnumIni
));
// Erro here. QObject::connect(vthrThread
[vnumIni
],
&clsThrTask
::sigRetorno, ui
->txtResultado,
&QTextEdit
::append);
// Erro here. vthrThread[vnumIni]->start();
}
1) error
: no matching function
for call to
'QThread::QThread(QString&)' vthrThread
[vnumIni
] = new QThread(vstrIdThread
);
2) error: invalid conversion from 'QThread*' to 'const Object* {aka const clsThrTask*}' [-fpermissive]
QObject::connect(vthrThread
[vnumIni
],
&clsThrTask
::sigRetorno, ui
->txtResultado,
&QTextEdit
::append);
^
//Arquivo MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
// ARRAY OF THREADS
QThread *vthrThread[vnumCont];
for(int vnumIni=0; vnumIni < vnumCont; vnumIni++)
{
vthrThread[vnumIni] = new QThread(QString::number(vnumIni)); // Erro here.
QObject::connect(vthrThread[vnumIni], &clsThrTask::sigRetorno, ui->txtResultado, &QTextEdit::append); // Erro here.
vthrThread[vnumIni]->start();
}
1) error: no matching function for call to 'QThread::QThread(QString&)' vthrThread[vnumIni] = new QThread(vstrIdThread);
2) error: invalid conversion from 'QThread*' to 'const Object* {aka const clsThrTask*}' [-fpermissive]
QObject::connect(vthrThread[vnumIni], &clsThrTask::sigRetorno, ui->txtResultado, &QTextEdit::append);
^
To copy to clipboard, switch view to plain text mode
Bookmarks