Handle QLineEdit inside script
I use QScript in my app in such manner:
I created object to pass it to script:
Code:
class ScriptMediator
:public QObject{
Q_OBJECT
public:
explicit ScriptMediator
(QWidget *parent
= 0);
{
list.append(child);
}
private:
QList<QWidget *> list;
public slots:
void okClicked();
void cancelClicked();
};
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:
Code:
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:
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?
Re: Handle QLineEdit inside script
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/qts...ipt-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.
Re: Handle QLineEdit inside script
Quote:
Originally Posted by
folibis
okButton and cancelButton slots work good but from returnPressed() I get only empty string.
this.text probably doesn't exist.
Quote:
Originally Posted by
folibis
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"?
Quote:
Originally Posted by
folibis
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,
_
Re: Handle QLineEdit inside script
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
Code:
function returnPressed()
{
Mediator.print( Mediator.GetWidget("code").text );
}
and it works good