Qt 5.10 Book Examples
main.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // main.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include <QJSEngine>
13 #include "JSTools.h"
14 
15 // ----------------------------------------------------------------------
16 int main(int argc, char** argv)
17 {
18  QApplication app(argc, argv);
19  QWidget* pwgt = new QWidget;
20 
21  QPushButton* pcmdFiles = new QPushButton("Show Files");
22  pcmdFiles->setObjectName("cmdFiles");
23 
24  QPushButton* pcmdQuit = new QPushButton("Quit");
25  pcmdQuit->setObjectName("cmdQuit");
26 
27  //Layout Setup
28  QVBoxLayout* pvbxLayout = new QVBoxLayout;
29  pvbxLayout->addWidget(pcmdFiles);
30  pvbxLayout->addWidget(pcmdQuit);
31  pwgt->setLayout(pvbxLayout);
32 
33  pwgt->show();
34 
35  //Script part
36  QJSEngine se;
37  QFile file(":/script.js");
38  if (file.open(QFile::ReadOnly)) {
39  QJSValue sw = se.newQObject(pwgt);
40 
41  se.globalObject().setProperty("wgt", sw);
42 
43  QList<QObject*> lst = pwgt->findChildren<QObject*>();
44  foreach(QObject* pobj, lst) {
45  sw = se.newQObject(pobj);
46  se.globalObject().setProperty(pobj->objectName(), sw);
47  }
48 
49  JSTools* pjt = new JSTools;
50  sw = se.newQObject(pjt);
51  QString strClassName = pjt->metaObject()->className();
52  se.globalObject().setProperty(strClassName, sw);
53 
54  QJSValue result =
55  se.evaluate(QLatin1String(file.readAll()));
56  if (result.isError()) {
57  QMessageBox::critical(0,
58  "Evaluating error",
59  result.toString(),
60  QMessageBox::Yes
61  );
62  }
63  }
64  else {
65  QMessageBox::critical(0,
66  "File open error",
67  "Can not open the script file",
68  QMessageBox::Yes
69  );
70  }
71 
72  return app.exec();
73 }
int main(int argc, char **argv)
Definition: main.cpp:15