Qt (9) How to create window with QtScript
Published on| June 22nd, 2009 | No Comment.
Categories:Qt |
Description: In Qt, java script as well as ECMA-262 compliant QtScript is implemented.
In other words, java script in the same grammatical features in writing the standard script.
In QtScript, java script and object recognition as part of the screen, you can also control the operation.
In this article, QtScript simple explanation of the behavior, then, QtScript Let's make the screen easy to use.
Articles: http://doc.trolltech.com/4.5/qtscript.html
Sample source code you used here:
Create a simple screen QtScript run.
First, QtScript Let's create a simple screen to confirm the operation.
Create screen, the following screen.

Script at the top of the screen, try to create output to the screen to the bottom of the final output.
The menu is the only run of the script.
File
- sample.cpp
- MainWindow I just skip the show. - mainwindow.h
- mainwindow.cpp
This description is omitted on the configuration screen.
The following is what happens when you click a menu execution of the script (slot) is a part.
[mainwindow.cpp]
1 2 3 4 5 6 7 8 9 10 | : : void MainWindow::excuteScript() { QString sscript=m_script-->toPlainText(); if(!sscript.isEmpty()){ QScriptEngine engine; m_output-->append( engine.evaluate(sscript).toString() ); } } |
Easily explained.
QTextEdit first string is entered (QtScript information) to obtain the information, if not empty, and run the script.
Run the script, "evaluate" method of the string of (QtScript information) will only run and pass.
Here are the results of m_output (the bottom QTextEdit) and output.
make time to note the following.
When you run qmake, or type the following, xxx.pro to "QT + = script" and add the.
Let's run a simple calculation from the screen.
When you run qmake, or type the following, xxx.pro to "QT + = script" and add the.
> qmake "QT+=script"
At the top of the screen, type the following, "Ctrl + G" type.

At the bottom of the screen, type the following, I think that is output.

This also more than just a simple tool to create a QtScript closed in, you may well.
Continues, Qt application QtScript try to parts of the output.
Let QtScript output debugging information.
QtScript in, if you output the information in the print function, you can use the debugging feature.
However, usually it is not able to see see output informations if you is not running a debugger, because QtScript will be output to the output of the debugger.
So, QtScript SHIMIMASHOU to output to the screen easily.
※ Normally, print and assign them to function independently, print but can be used, here, QtScript Qt and to describe the integration of applications and to achieve in different ways.
QtScript parts to the screen (m_output (the bottom QTextEdit)) to recognize, to try out for parts of the script.
[mainwindow.cpp]
1 2 3 4 5 6 7 8 9 10 11 12 | : : void MainWindow::excuteScript() { QString sscript=m_script-->toPlainText(); if(!sscript.isEmpty()){ QScriptEngine engine; QScriptValue scriptTextEdit = engine.newQObject(m_output); engine.globalObject().setProperty("debugPrint", scriptTextEdit); m_output-->append( engine.evaluate(sscript).toString() ); } } |
Based on the above lines, line, line 9, only two lines were added.
QScriptEngine, as a new object m_output (the bottom QTextEdit) to declare in the name of the object "debugPrint" is defined.
In other words, m_output (the bottom QTextEdit) QtScript in order to access the "debugPrint" is having access to a written directive.
Allows you to write a simple script from this screen, we have been printed.
At the top of the screen, type the following, "Ctrl + G" type.

At the bottom of the screen, type the following, I think that is output.

"undefined" means that none of the results of the script.
(In the script above, simply m_output (the bottom QTextEdit) not only results in the output. )
Typically, print statement, if you want to output to the screen of their own conduct as follows.
This is not a function object (function) and a new definition, the name "print" and.
Also, the function is associated with m_output.
Now, QtScript in the "print" and stating, m_output output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | QScriptValue scriptPrintFunction(QScriptContext *context, QScriptEngine *engine) { QString result; for (int i = 0; i < context-->argumentCount(); ++i) { if (i > 0){ result.append(" "); } result.append(context-->argument(i).toString()); } QScriptValue calleeData = context-->callee().data(); QTextEdit *edit = qobject_cast<QTextEdit*>(calleeData.toQObject()); edit-->append(result); return engine-->undefinedValue(); } : : QScriptValue printfunc = m_engine-->newFunction(scriptPrintFunction); printfunc.setData(m_engine-->newQObject(m_output)); m_engine-->globalObject().setProperty("print", printfunc); |
This is not a function object (function) and a new definition, the name "print" and.
Also, the function is associated with m_output.
Now, QtScript in the "print" and stating, m_output output.
Thus Qt application screen can be used in QtScript.
So, to continue, Qt applications are not on the screen, QtScript try to create.
Pages: 1 2
You might also like:
Trackback URL
After Admin approves this comment, it will be shown.
Comments
Leave a Reply
