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.
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.
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.
QtScript Qt application that DEKIRO screen to use is described above.
So, you can use to check the range.
The class that inherits QObject, signals, slots, properties can be used.
In addition, the usual method, the top "Q_INVOKABLE" can be used to specify the keyword.
Simple, QTextEdit to create an inherited class, append Let's just have a method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| #ifndef DEBUGWIDGET_H
#define DEBUGWIDGET_H
#include <QtGui>
#include <QtScript>
class DebugWidget : public QTextEdit
{
Q_OBJECT
public :
DebugWidget(QWidget *parent=0);
Q_INVOKABLE void output_invoke(const QString &text) { append(text); };
void output_noinvoke(const QString &text) { append(text); };
protected :
Q_INVOKABLE void output2_invoke(const QString &text) { append(text); };
private :
Q_INVOKABLE void output3_invoke(const QString &text) { append(text); };
public slots:
void output(const QString &text) { append(text); };
protected slots:
void output2(const QString &text) { append(text); };
private slots:
void output3(const QString &text) { append(text); };
};
#endif
|
non output_noinvoke, public, protected, private is only one difference.
Let's run each.
debugPrint.output("test");
debugPrint.output2("test");
debugPrint.output3("test");
debugPrint.output_invoke("test");
debugPrint.output2_invoke("test");
debugPrint.output3_invoke("test");
debugPrint.output_noinvoke("test");
The last to fail.
In other words, public, protected, private, etc., you can use any at all.
properties does not appear much before.
The idea is, VCL is identical.
How to register, register below.
(QWidget that can be used in all classes inherit)
class DebugWidget : public QTextEdit
{
Q_OBJECT
Q_PROPERTY( bool visible WRITE setVisible READ isVisible )
:
In this example, "visible" property is defined.
When writing, "setVisible" using a method that, when read, "isVisible" we have declared the method to use.
When you declare this way, QtScript in can be described as follows.
if(obj.visible==false) { // =isVisible()
obj.visible = true; // =show
} else {
obj.visible = false; // =hide
}
QtScript create dialog
The use of object methods, I think I understand most.
Immediately, QtScript Let's create a dialogue.
Qt in advance for what was available in the app, I think I understand.
How to create new object of windows with QtScript?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| Q_SCRIPT_DECLARE_QMETAOBJECT(QDialog, QWidget*)
:
:
void MainWindow::excuteScript()
{
QString sscript=m_script->toPlainText();
if(!sscript.isEmpty()){
QScriptEngine engine;
QScriptValue objectwin= engine.scriptValueFromQMetaObject<QDialog>();
engine.globalObject().setProperty("Dialog", objectwin);
m_output->append( engine.evaluate(sscript).toString() );
}
}
|
cpp at the top, QtScript in, Meta and the use of the object declaration.
QScriptEngine, Meta and create a new object, the name "Dialog" has been declared.
At the top of the screen, type the following, "Ctrl + G" Let's run.
I think the big screen the following dialog.
Here is where you want to specify the screen size, slot in the method to change the screen size.
So, QDialog inherits the class, you can specify the screen size and position setGeometry we will call a public method.
1
2
3
4
5
6
7
8
9
10
11
| class SDialog : public QDialog
{
Q_OBJECT
public:
SDialog(QWidget *parent) : QDialog(parent) {};
public slots:
void setPosition(int x, int y, int w, int h ){
setGeometry ( x, y, w, h );
};
};
|
With this class, "setPosition" QtScript that will be available.
At the top of the screen, type the following, "Ctrl + G" Let's run.
I think the small screen that displays the following dialog.
In this way, QLabel, QPushButton, QTextEdit as a QtScript will be able to use in the following script, you can create a simple screen.
var win=new Dialog;
var btn=new Button(win);
var txt=new TextEdit(win);
var lbl=new Label(win);
win.setPosition(100,100,200,100);
btn.setPosition(150,40,40,25);
txt.setPosition(10,40,130,25);
lbl.setPosition(10,15,130,25);
lbl.setText("Hellow Script Window!!");
win.exec();
Screen is created by the script above.
QtScript to control the signals and slots in
Finally, QtScript slot and try to control the signals created by the screen.
As also described earlier, QtScript is in, QObject class inherited the signals, slots, properties can be used.
In other words, we can control the signals and slots.
So, connect the way, desiconnect, signal to the cause will do.
QtScript in, connect to, signal as a member.
For example, PushButton Click the signal is as follows.
var btn=new Button(win);
btn.clicked.connect(cleckButton);
Here, PushButton Click cleckButton function of the signal (function) and to connect.
You can disconnect as well.
var btn=new Button(win);
btn.clicked.connect(cleckButton);
:
:
btn.clicked.disconnect(cleckButton);
When the signal generator, signal only for a method call.
var btn=new Button(win);
btn.clicked();
, The dialog screen where you create the script, if you click the button, TextEdit to "append" Let.
var win=new Dialog;
var btn=new Button(win);
var txt=new TextEdit(win);
var lbl=new Label(win);
win.setPosition(100,100,200,100);
btn.setPosition(150,40,40,25);
txt.setPosition(10,40,130,25);
lbl.setPosition(10,15,130,25);
lbl.setText("Hellow Script Window!!");
function cleckButton()
{
txt.append("Clicked!!");
}
btn.clicked.connect(cleckButton);
print(win.exec());
Screen is created by the script above.
When you click the right button to TextEdit, "Clicked!!" I see it.
QtScript is good.
If a simple tool, this script will be realized.
In addition, Qt to build the application, it can be more interesting.
Finally QtScript also has debugging tools.
If you run debug, and execution steps.
The sample source is included to perform debugging, please help.
Leave a Reply