PDA

View Full Version : QtScript encoding



folibis
17th October 2013, 11:36
In my program I use QDialog class and QtScript inside it for business logic layer (validation etc.)
Usually a message from JS script passes to C++ program and displays with QMessageBox. Message text in Russian and what I see is abracadabra.
Qt internally uses UTF-8 as I understand. All JS scripts written in UTF-8.
I did'nt found in documentation how to set encoding for scripts.
So what I do wrong?

toufic.dbouk
17th October 2013, 12:08
Do you see abracadabra language in your QMessageBox?

anda_skoa
17th October 2013, 12:14
QString is UTF-16 internally, but the bridge to JS should take care of that and I guess assuming UTF-8 on the JS side is what it does.

Can you attach a ZIP with a minimal project that has such a script file and calls a C++ method?

Cheers,
_

folibis
18th October 2013, 01:05
It is too complicated to get a part of the project. :) I tried but this Dialog uses global objects and code from other files :)
So I changed encoding of JS file to cp1251 and now it works as it should be.
I guess the script engine uses system encoding

anda_skoa
18th October 2013, 10:00
Interesting. How did you load the script?

Cheers,
_

folibis
18th October 2013, 13:20
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());
}

where ScriptMediator defined as:

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);
}

ShowMessage it is just my replacement for QMessageBox:


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;
}

where Message class defined as:

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);
}
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);

In my code "Form validate error" written in Russian and I see it as abracadabra

anda_skoa
18th October 2013, 16:27
I might be overlooking it but where do you load the script into "contents"?

Cheers,
_

folibis
19th October 2013, 04:09
Ehhm ... missed it. I load it just before
QScriptValue objectValue = p_engine.newQObject(&p_mediator);


QFile scriptFile(qApp->applicationDirPath() + "/script.qs");
if (!scriptFile.open(QIODevice::ReadOnly)) return;
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();

anda_skoa
19th October 2013, 14:57
Ah, so you where reading the file content but not using UTF-8 as the encoding when converting to QString.



QString contents = QString::fromUtf8( stream.readAll() );


Cheers,
_

folibis
20th October 2013, 23:42
This code will not work. QTextStream::readAll() returns QString but QString::fromUtf8() accepts char *.
But I understand your train of thought.


QString fileName = qApp->applicationDirPath() + "/script.qs";
QFile scriptFile(fileName);
QTextStream stream(&scriptFile);
stream.setCodec("UTF-8"); // <--
QString contents = stream.readAll();
QScriptValue result = p_engine.evaluate(contents, fileName);

it works on condition that JS script encoded with UTF-8, so I come to a conclusion than QScriptEngine don't care for encodings and loads script as is.

anda_skoa
21st October 2013, 21:33
This code will not work. QTextStream::readAll() returns QString but QString::fromUtf8() accepts char *.

Ah, yes, I didn't look closely enough and assumed QFile::readAll() :)



it works on condition that JS script encoded with UTF-8, so I come to a conclusion than QScriptEngine don't care for encodings and loads script as is.
Right. The script will always be UTF-16 as far as the script engine is concerned since it always gets it as a QString.

Cheers,
_