Results 1 to 11 of 11

Thread: QtScript encoding

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QtScript encoding

    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?

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    Do you see abracadabra language in your QMessageBox?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    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,
    _

  4. #4
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    Interesting. How did you load the script?

    Cheers,
    _

  6. #6
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    it almost copy/paste from documentation:

    Qt Code:
    1. QScriptEngine p_engine;
    2. ScriptMediator p_mediator;
    3. ...
    4. QScriptValue objectValue = p_engine.newQObject(&p_mediator);
    5. p_engine.globalObject().setProperty("Mediator", objectValue);
    6. QScriptValue result = p_engine.evaluate(contents, fileName);
    7. if(result.isError())
    8. {
    9. ShowMessage("Script error",result.toString());
    10. }
    To copy to clipboard, switch view to plain text mode 

    where ScriptMediator defined as:
    Qt Code:
    1. class ScriptMediator :public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ScriptMediator(QWidget *parent = 0);
    6.  
    7. public slots:
    8. void okClicked();
    9. void cancelClicked();
    10. void print(QString string);
    11. };
    12.  
    13. void ScriptMediator::print(QString string)
    14. {
    15. ShowMessage("Message from script",string);
    16. }
    To copy to clipboard, switch view to plain text mode 

    ShowMessage it is just my replacement for QMessageBox:
    Qt Code:
    1. bool Config::ShowMessage(QString title, QString message, QWidget *parent)
    2. {
    3. bool retval = false;
    4. Message * dlg = new Message(title,message,parent);
    5. if(dlg->exec() == QDialog::Accepted) retval = true;
    6. delete dlg;
    7. return retval;
    8. }
    To copy to clipboard, switch view to plain text mode 

    where Message class defined as:
    Qt Code:
    1. class Message : public QDialog
    2. {
    3. public:
    4. explicit Message(QString title,QString message,QWidget *parent = 0);
    5. ...
    6. }
    7.  
    8. Message::Message(QString title, QString message, QWidget *parent) :
    9. QDialog(parent),
    10. ui(new Ui::Message)
    11. {
    12. ui->setupUi(this);
    13. this->setWindowTitle(title);
    14. ui->message->setText(message);
    15. }
    To copy to clipboard, switch view to plain text mode 
    simple form with only QLabel on it
    and JS script:
    Qt Code:
    1. function validate()
    2. {
    3. return false;
    4. }
    5. function okClicked()
    6. {
    7. if(validate()) Mediator.okClicked();
    8. else Mediator.print( "Form validate error" );
    9. }
    10. 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

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    I might be overlooking it but where do you load the script into "contents"?

    Cheers,
    _

  8. #8
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    Ehhm ... missed it. I load it just before
    QScriptValue objectValue = p_engine.newQObject(&p_mediator);

    Qt Code:
    1. QFile scriptFile(qApp->applicationDirPath() + "/script.qs");
    2. if (!scriptFile.open(QIODevice::ReadOnly)) return;
    3. QTextStream stream(&scriptFile);
    4. QString contents = stream.readAll();
    5. scriptFile.close();
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    Ah, so you where reading the file content but not using UTF-8 as the encoding when converting to QString.

    Qt Code:
    1. QString contents = QString::fromUtf8( stream.readAll() );
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  10. #10
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    This code will not work. QTextStream::readAll() returns QString but QString::fromUtf8() accepts char *.
    But I understand your train of thought.

    Qt Code:
    1. QString fileName = qApp->applicationDirPath() + "/script.qs";
    2. QFile scriptFile(fileName);
    3. QTextStream stream(&scriptFile);
    4. stream.setCodec("UTF-8"); // <--
    5. QString contents = stream.readAll();
    6. QScriptValue result = p_engine.evaluate(contents, fileName);
    To copy to clipboard, switch view to plain text mode 

    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtScript encoding

    Quote Originally Posted by folibis View Post
    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()

    Quote Originally Posted by folibis View Post
    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,
    _

Similar Threads

  1. Encoding Audioinput
    By nwz in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2012, 18:28
  2. Encoding/Decoding and SQL
    By alexandernst in forum Qt Programming
    Replies: 0
    Last Post: 1st February 2011, 12:16
  3. encoding problem
    By lexqqq in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2010, 00:41
  4. Replies: 0
    Last Post: 25th November 2009, 08:46
  5. How can I get a filename from a different encoding?
    By Pepe in forum Qt Programming
    Replies: 4
    Last Post: 28th March 2007, 01:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.