PDA

View Full Version : Qtscript -signals and slots



coolmac
16th July 2008, 06:27
Hi all,
I have just started programming using Qt4.4.
I was trying the example given in
http://labs.trolltech.com/blogs/2007/07/17/connecting-to-qtscript-functions-from-c/
The only thing I have changed here is that I have defined a class MyWindow which incorporates the window,awakenButton and exitButton.
I was successful in connecting javascriptfile using a signal(buttonClicked()) defined in MyWindow class to a javascript file using
qScriptConnect(obj1, SIGNAL(buttonClicked()), object, slot);
obj1-object of MyWindow.

I want to connect to a signal defined in MyWindow class from javascript file and emit this signal in the same scriptfile so I tried using the syntax given in QtScript Module (Qt4.4)but it didnt work. Is there any specific way to connect to a signal from scriptfile?

Also,once the connection is established, can we initialize objects in scriptfile like
var obj2=new MyWindow;
I tried this but this also wasnt working.
Is this possible and is this the right way?

Thnx in advance.

coolmac
16th July 2008, 16:02
Hi again folks...
I am just giving you an idea of MyWindow class..



/********************MyWindow.h******************** ****/
class MyWindow: public QObject,public QScriptable
{
Q_OBJECT
public:
Window();

public slots :
void changeButton();
void clickEvent();
void changeSumthing();
void getexitButton();



private:
QWidget window;
QPushButton *awakenButton;
QPushButton *exitButton;
signals:
void buttonClicked();

};


/****************main.cpp********************/
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyWindow *window1=new Window;
QScriptEngine engine;
QScriptValue object = engine.newObject();
object.setProperty("name", QScriptValue(&engine, "Megatron"));
QFile file("window.js");
bool t=file.open(QIODevice::ReadOnly);
QScriptValue slot = engine.evaluate(file.readAll());
QString str=slot.toString();
file.close();
//connecting to scriptfile using signal buttonClicked(emitted in clickEvent()slot )
qScriptConnect(window1, SIGNAL(buttonClicked()), object, slot);
return app.exec();
}

Lastly my window.js file is:

/*********window.js************/
return function() {
var sender = __qt_sender__;
sender.value=1;
sender.changeButton();
//tried connecting the exitButton click signal to the slot changeSumthing..but not working
sender.exitButton.clicked.connect(sender,"changeSumthing");
//Also can we initialize objects here..and then access the methods
var obj2=new MyWindow;
}

I hope this will help u to understand my problem better..