SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. QT += core qml quick quickcontrols2
  2. TARGET = qjsengine-bug
  3. TEMPLATE = app
  4. DEFINES += QT_DEPRECATED_WARNINGS
  5. CONFIG += c++11 console
  6. SOURCES += main.cpp
  7.      
  8. #include <QCoreApplication>
  9. #include <QJSValueIterator>
  10. #include <QJSEngine>
  11. #include <QJSValue>
  12. #include <QtGlobal>
  13.  
  14. #include <iostream>
  15.  
  16. void myMessageOutput(
  17.     QtMsgType t,
  18.     const QMessageLogContext &c,
  19.     const QString &msg
  20. ) {
  21.     Q_UNUSED(t);
  22.     Q_UNUSED(c);
  23.     std::cout << msg.toStdString() << "n";
  24. }
  25.  
  26. int main(int argc, char *argv[]) {
  27.     qInstallMessageHandler(myMessageOutput);
  28.     QCoreApplication app(argc, argv);
  29.  
  30.     QJSEngine engine;
  31.     engine.installExtensions(QJSEngine::ConsoleExtension);
  32.     QJSValue constructor = engine.evaluate(
  33.         "(function Component(props){console.log(JSON.stringify(props))})"
  34.     );
  35.  
  36.     QJSValue callBack = engine.evaluate("(function(text){console.log(text)})");
  37.     callBack.call({"There is no error. Valid JavaScript code..."});
  38.  
  39.     callBack.call({"Let's create an object, add a couple of props to it"});
  40.  
  41.     QJSValue object = engine.newObject();
  42.     object.setProperty("First", 1);
  43.     object.setProperty("Second", callBack);
  44.     object.setProperty("Third", "#2");
  45.  
  46.     QJSValueIterator iter(object);
  47.     while (iter.hasNext()) {
  48.         iter.next();
  49.         callBack.call({
  50.             QString("name: %1, value: %2")
  51.                 .arg(iter.name())
  52.                 .arg(iter.value().toString())
  53.         });
  54.     }
  55.  
  56.     callBack.call({"Correct. Three fields"});
  57.     callBack.call({"Let's try to pass an object to the constructor parameters"});
  58.  
  59.     constructor.callAsConstructor({object});
  60.  
  61.     callBack.call({"Where did the second property go?"});
  62.  
  63.     return app.exec();
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top