-1

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.

SWR
  • 123
  • 6

1 Answers1

0

You need to make more connections. You start() the thread, but that does nothing, it executes the thread's default run() function that doesn't do anything unless you overload it to. You need to connect the thread's started() signal to your class Started() function.

I think there is a problem with sleeping. Sleeping blocks the thread, and you need the thread running so that it event loop is spinning, because the event loop is what does the communication between objects in different threads.

See here for an example of a non-blocking worker. Note that you are not longer using loops directly, but need to decompose them into abstract loops that run along the event loop.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • i deleted all "Sleep-rows", but nothing is changed( – SWR Oct 08 '17 at 11:29
  • i have this row in my code : connect(thread, SIGNAL(started()), newClass, SLOT(Start())); i forgot this in my question; – SWR Oct 08 '17 at 11:53
  • in my Start() function, if i'll remove my loop and write only this row - "emit sendfirstvalue(for example value=23);" , nothing is changed too – SWR Oct 08 '17 at 11:58