PDA

View Full Version : Handle QLineEdit inside script



folibis
14th October 2013, 06:07
I use QScript in my app in such manner:

I created object to pass it to script:

class ScriptMediator :public QObject
{
Q_OBJECT
public:
explicit ScriptMediator(QWidget *parent = 0);
void AddWidget(QWidget * child)
{
list.append(child);
}
private:
QList<QWidget *> list;
public slots:
void okClicked();
void cancelClicked();
void print(QString string);
QWidget * GetWidget(QString name);

};

In short, I want to pass all controls from specified form to script.

after dialog creation I initialize script and pass this object to script:




p_mediator.AddWidget(ui->okButton); // QPushButton
p_mediator.AddWidget(ui->cancelButton); // QPushButton
p_mediator.AddWidget(ui->edit); // QLineEdit

QScriptValue objectValue = p_engine.newQObject(&p_mediator);
p_engine.globalObject().setProperty("Mediator", objectValue);
QScriptValue result = p_engine.evaluate(contents, fileName);

Script code:


function okClicked()
{
Mediator.okClicked();
}
function cancelClicked()
{
Mediator.cancelClicked();
}
function returnPressed()
{
Mediator.print( this.text );
}

Mediator.GetWidget("okButton").clicked.connect(okClicked);
Mediator.GetWidget("cancelButton").clicked.connect(cancelClicked);
Mediator.GetWidget("code").returnPressed.connect(returnPressed);

okButton and cancelButton slots work good but from returnPressed() I get only empty string.
if i rewrite it to Mediator.print( this.text() ); (calling to text() function, not to property) I get error "this.text is not a function".

So how can I use QLineEdit inside a script to get a text from it?

pkj
14th October 2013, 07:43
If you go through QLineEdit , the text is a property. So use it as a property of javascript wrapper of QlineEdit in script. Not as a function. For a function to be introduced, it should be preceded with Q_INVOKABLE....
Next, in the code you have written for your mediator, I don't see a returnPressed slot. So the connect in you script will fail... You might even be getting a connect failed message if you have not attached the qtscriptdebugger.
Thirdly in your script I see a function using the this context.
What is the context you are calling the function from? If it is a global context, this signifies global object in javascript. The function should be called from the context of QLineEdit. But you never introduced QLineEdit to javascript, but only the mediator. So get the context of this right before calling the function. Go through http://qt-project.org/doc/qt-5.0/qtscript/qtscript-index.html the document in link for more details.

Added after 10 minutes:

Basically you need
Signal to Named Member Function Connections

connect(thisObject, functionName)

form of connection. In the document in the above link, see section Using Signals and slots.

anda_skoa
14th October 2013, 12:20
okButton and cancelButton slots work good but from returnPressed() I get only empty string.


this.text probably doesn't exist.



if i rewrite it to Mediator.print( this.text() ); (calling to text() function, not to property) I get error "this.text is not a function".

Do you have a text() function in the context that is referred to by "this"?



So how can I use QLineEdit inside a script to get a text from it?

Have you tried reading the line edit's text property?

Cheers,
_

folibis
14th October 2013, 12:41
I supposed that "this" in context of slot function points to sender object but it is not true. "this" points to Global object.
I changed it to

function returnPressed()
{
Mediator.print( Mediator.GetWidget("code").text );
}
and it works good