I'm trying to change the value of a text field from another thread, but for some reason it does not work: There are 2 windows, the first window:
firstwindow.h:
#include "mythread.h" #include <QObject> class FirstWindow : public QObject { Q_OBJECT public: explicit FirstWindow(QObject *parent = nullptr); private: MyThread *myThread; public slots: void start(); }; firstwindow.cpp:
#include "firstwindow.h" FirstWindow::FirstWindow(QObject *parent) : QObject(parent) { } void FirstWindow::start() { myThread = new MyThread(5); //передача значения в класс MyThread } firstwindow.qml:
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import QtQuick.Window 2.0 import FirstWindowModule 1.0 ApplicationWindow { visible: true width: 380 height: 240 id: firstwindow FirstWindow { id: obj; } //создание и вызов второго окна, вызов функции start(), создающей объект класса MyThread Button { text: "Start" onClicked: { var component = Qt.createComponent("main.qml"); console.log("Component Status:", component.status, component.errorString()); var window = component.createObject(firstwindow); window.show() obj.start(); } } } Second window:
mythread.h:
#include "newclass.h" #include <QObject> #include <QThread> class MyThread : public QObject { Q_OBJECT Q_PROPERTY(QString firstNumber READ GetFirstNumber WRITE SetFirstNumber NOTIFY firstNumberChanged) private: QThread *thread; NewClass *newClass; QString firstNumber; int counter; private slots: void UpdateFirstValue (int i); public slots: void StartThread(int); public: explicit MyThread(int value, QObject *parent = nullptr); explicit MyThread(QObject *parent = nullptr); QString GetFirstNumber(); void SetFirstNumber(QString); signals: void firstNumberChanged(); }; mythread.cpp:
#include "mythread.h" MyThread::MyThread(QObject *parent) : QObject(parent) { } MyThread::MyThread(int counter, QObject *parent) : QObject(parent) { this->counter = counter; StartThread(counter); //запуск функции создания и запуска потока } QString MyThread::GetFirstNumber() { return firstNumber; } void MyThread::SetFirstNumber(QString value) { firstNumber = value; } void MyThread::StartThread(int counter) { thread = new QThread; newClass = new NewClass(counter); newClass->moveToThread(thread); connect(newClass, SIGNAL(sendfirstvalue(int)), this, SLOT(UpdateFirstValue(int))); connect(thread, SIGNAL(started()), newClass, SLOT(Start())); thread->start(); } void MyThread::UpdateFirstValue (int i) { firstNumber = QString::number(i); emit firstNumberChanged(); } main.qml:
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import QtQuick.Window 2.0 import NameModule 1.0 ApplicationWindow { visible: true width: 380 height: 240 Rectangle { id: content TypeName { id: obj } ColumnLayout { height: parent.height anchors.horizontalCenter: content.horizontalCenter Rectangle { Layout.fillHeight: true Text { anchors.centerIn: parent id: firstNumber text: "f = " + obj.firstNumber font.bold: true onTextChanged: { console.log("firstNumberChanged"); } } } } } } The NewClass class, whose function works in the stream:
newclass.h:
#include <QObject> class NewClass : public QObject { Q_OBJECT public: explicit NewClass(int value, QObject *parent = nullptr); signals: void sendfirstvalue(int); private slots: void Start(); private: int value; }; newclass.cpp:
#include "newclass.h" #include <windows.h> #include "QDebug" NewClass::NewClass(int value, QObject *parent) : QObject(parent) { this->value = value; } void NewClass::Start() { qDebug() << "value" << value; for(int i = 0; i < value; i++) { qDebug() << "i" << i; emit sendfirstvalue(i); Sleep(1000); // from windows.h } } A bunch of FirstWindow and MyThread classes with their corresponding qml:
qmlRegisterType<FirstWindow>("FirstWindowModule", 1, 0, "FirstWindow"); qmlRegisterType<MyThread>("NameModule", 1, 0, "TypeName"); The essence of the problem: 2 windows, in the first window (FirstWindow) there is a button that opens the second window (MyThread), and immediately starts the computation process in the new thread (the Start () function in the NewClass class. This function loops, every second sends an integer value back to the second MyThread window, and this value should be displayed in this window. It was determined that the problem is most likely in the creation of this second window, that either the thread starts before the window is shown, or something else. Thank you.
UPDATE: rewrote the question again and uploaded the project to https://github.com/sawyerhard/test2 . Problem not solved