it almost copy/paste from documentation:
QScriptEngine p_engine;
ScriptMediator p_mediator;
...
QScriptValue objectValue = p_engine.newQObject(&p_mediator);
p_engine.globalObject().setProperty("Mediator", objectValue);
QScriptValue result = p_engine.evaluate(contents, fileName);
if(result.isError())
{
ShowMessage("Script error",result.toString());
}
QScriptEngine p_engine;
ScriptMediator p_mediator;
...
QScriptValue objectValue = p_engine.newQObject(&p_mediator);
p_engine.globalObject().setProperty("Mediator", objectValue);
QScriptValue result = p_engine.evaluate(contents, fileName);
if(result.isError())
{
ShowMessage("Script error",result.toString());
}
To copy to clipboard, switch view to plain text mode
where ScriptMediator defined as:
class ScriptMediator
:public QObject{
Q_OBJECT
public:
explicit ScriptMediator
(QWidget *parent
= 0);
public slots:
void okClicked();
void cancelClicked();
};
void ScriptMediator
::print(QString string
) {
ShowMessage("Message from script",string);
}
class ScriptMediator :public QObject
{
Q_OBJECT
public:
explicit ScriptMediator(QWidget *parent = 0);
public slots:
void okClicked();
void cancelClicked();
void print(QString string);
};
void ScriptMediator::print(QString string)
{
ShowMessage("Message from script",string);
}
To copy to clipboard, switch view to plain text mode
ShowMessage it is just my replacement for QMessageBox:
{
bool retval = false;
Message * dlg = new Message(title,message,parent);
if(dlg
->exec
() == QDialog::Accepted) retval
= true;
delete dlg;
return retval;
}
bool Config::ShowMessage(QString title, QString message, QWidget *parent)
{
bool retval = false;
Message * dlg = new Message(title,message,parent);
if(dlg->exec() == QDialog::Accepted) retval = true;
delete dlg;
return retval;
}
To copy to clipboard, switch view to plain text mode
where Message class defined as:
{
public:
...
}
ui(new Ui::Message)
{
ui->setupUi(this);
this->setWindowTitle(title);
ui->message->setText(message);
}
class Message : public QDialog
{
public:
explicit Message(QString title,QString message,QWidget *parent = 0);
...
}
Message::Message(QString title, QString message, QWidget *parent) :
QDialog(parent),
ui(new Ui::Message)
{
ui->setupUi(this);
this->setWindowTitle(title);
ui->message->setText(message);
}
To copy to clipboard, switch view to plain text mode
simple form with only QLabel on it
and JS script:
function validate()
{
return false;
}
function okClicked()
{
if(validate()) Mediator.okClicked();
else Mediator.print( "Form validate error" );
}
Mediator.okButton.clicked.connect(okClicked);
function validate()
{
return false;
}
function okClicked()
{
if(validate()) Mediator.okClicked();
else Mediator.print( "Form validate error" );
}
Mediator.okButton.clicked.connect(okClicked);
To copy to clipboard, switch view to plain text mode
In my code "Form validate error" written in Russian and I see it as abracadabra
Bookmarks