Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

multithreading - Is there a way to run C++ from a QML WorkerScript?

I have a slow I/O operation that I need to control from a QML UI. The I/O interface is in C++. Basically, when a user presses a button, I need to send a message and get a response from the device. I want the user to be able to do other things while waiting for a response. WorkerScript seems like the easiest way to make this happen, but how do I get my C++ interface into the script since the normal QDeclarativeContext doesn't pass into the thread? Is there a way to import C++ into QML's javascript? I don't even need to maintain the C++ context in the main thread, I'd be fine with it living entirely in the worker and just passing messages back and forth.
EDIT:
Clarifications: @dtech's answer satisfies my current need, but I would still like to know the answer to the question: is it possible to get C++ (even if not stateful) into a WorkerScript.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why would you do that when you have the option to put QObjects into dedicated threads, execute code without blocking the main thread, and communicate and deliver results back and forth to QML asyncronously?

You don't need WorkerScript, nor is this its intended use. And since your code is C++ anyway, all you need is QThread and QObject.

Here is a simple example:

class Worker : public QObject {
    Q_OBJECT
  public slots:
    void doWork() {
      int i = 0;
      while (i < 100) {
        result(i++);
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
      }
    }
  signals:
    void result(int r);
};

class Controller : public QObject {
    Q_OBJECT
  public:
    Controller() {
      w = new Worker;
      t = new QThread;
      w->moveToThread(t);
      connect(this, SIGNAL(start()), w, SLOT(doWork()));
      connect(w, SIGNAL(result(int)), this, SIGNAL(result(int)));
      t->start();
    }
  private:
    Worker * w;
    QThread * t;
  signals:
    void start();
    void result(int r);
};

// in main.cpp
  Controller cw;
  engine.rootContext()->setContextProperty("Work", &cw);
  engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); // load main qml


// QML

  Column {
    Button {
      id: start
      onClicked: Work.start()
    }
    Text {
      id: res
    }
  }

  Connections {
    target: Work
    onResult: res.text = r
  }

It is a simple blocking worker that will block its thread for about 50 seconds, but nonetheless will be able to emit results that will be updated on the QML side, while keeping the GUI thread free. Note that once the work function is invoked, it is not possible to interrupt, pause, or control it in any way, if that is required, you will have to implement a non-blocking worker instead. Also not the need of a C++ controller to be present to act as a mediator between QML and the "threaded object", as it seems that QML doesn't get along with such objects directly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...