Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt (9) How to create window with QtScript

Published on| June 22nd, 2009 | No Comment.
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.




Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ