I'm trying to change text field from new thread, but cant( I have class MyThread:
class MyThread : public QObject
{
Q_OBJECT
Q_PROPERTY(QString firstNumber READ GetFirstNumber WRITE SetFirstNumber NOTIFY firstNumberChanged)
private:
QThread *thread;
NewClass *newCLass;
QString firstNumber;
private slots:
void StartThread();
void UpdateFirstValue (int i);
public:
explicit MyThread(QObject *parent = nullptr);
QString GetFirstNumber();
void SetFirstNumber(QString);
signals:
void firstNumberChanged();
}
Function for starting thread:
void MyThread::StartThread()
{
thread = new QThread;
newClass = new NewClass();
newClass->moveToThread(thread);
connect(newClass, SIGNAL(sendfirstvalue(int)), this, SLOT(UpdateFirstValue(int)));
connect(thread, SIGNAL(started()), newClass, SLOT(Start()));
thread->start();
}
Changing text field:
void MyThread::UpdateFirstValue (int i)
{
firstNumber = QString::number(i);
emit firstNumberChanged();
}
function in NewClass, that is working in thread:
void NewClass::Start()
{
for(int i = 0; i < 3; i++)
{
emit sendfirstvalue(i);
Sleep(1000);
}
}
Binding class MyThread with QML:
qmlRegisterType<MyThread>("NameModule", 1, 0, "TypeName");
qml file:
import QtQuick 2.0
import QtQuick.Layouts 1.1
import NameModule 1.0
Rectangle {
id: content
TypeName{
id: obj
}
ColumnLayout {
height: parent.height
anchors.horizontalCenter: content.horizontalCenter
Rectangle {
Layout.fillHeight: true
Text {
anchors.horizontalCenter: parent.horizontalCenter
id: firstNumber
text: "f = " + obj.firstNumber
font.bold: true
onTextChanged: {
console.log("firstNumberChanged");
}
}
}
}
Function is working, value of firstNumber is changing, but it itn't showing on the screen, thanks.