Results 1 to 5 of 5

Thread: Qt Script add method

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Qt Script add method

    Hey there,

    Here is a bit advanced question,

    I'm using the following routine to enable my QtScript to new a QLineEdit:

    Qt Code:
    1. Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
    2.  
    3. ...
    4.  
    5. QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
    6. engine.globalObject().setProperty("QLineEdit", lineEditClass);
    To copy to clipboard, switch view to plain text mode 

    Now I want to make one of this QLineEdit's member (not a slot, not a property) available in the script:

    example: "void QLineEdit::deselect ()"

    and in the script

    Qt Code:
    1. lineEdit = new QLineEdit;
    2. lineEdit.deselect();
    To copy to clipboard, switch view to plain text mode 

    What's the most simple way to achieve that.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Script add method

    Either add a function to the prototype of the QLineEdit class (in QScript) that will call deselect on the real object or subclass QLineEdit and use the Q_INVOKABLE macro.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    bunjee (10th May 2009)

  4. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Qt Script add method

    Thanks Wysota,

    The best solution is using "setDefaultPrototype" so it applies to every inheriting objects.

    Here is the implementation.

    (Courtesy of Kent himself from Trolltech).
    Qt Code:
    1. #include <QtScript>
    2. #include <QtGui>
    3.  
    4. static QScriptValue QWidget_prototype_move(QScriptContext *ctx, QScriptEngine *eng)
    5. {
    6. QWidget *widget = qscriptvalue_cast<QWidget*>(ctx->thisObject());
    7. Q_ASSERT(widget != 0);
    8. widget->move(ctx->argument(0).toInt32(), ctx->argument(1).toInt32());
    9. return eng->undefinedValue();
    10. }
    11.  
    12. int main(int argc, char **argv)
    13. {
    14. QApplication app(argc, argv);
    15.  
    16. QScriptEngine eng;
    17. QScriptValue widgetProto = eng.newObject();
    18. widgetProto.setProperty("move", eng.newFunction(QWidget_prototype_move));
    19. eng.setDefaultPrototype(qMetaTypeId<QWidget*>(), widgetProto);
    20.  
    21. win.resize(400, 400);
    22. QScriptValue scriptWindow = eng.newQObject(&win);
    23. scriptWindow.property("move").call(scriptWindow, QScriptValueList() << 100 << 200);
    24.  
    25. win.show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Sep 2012
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Qt Script add method

    Hi all,
    In my project, I need to espose to the script, the move slot(normaly privated) of all widgets that I use.
    I've implemented a solution that use QUiLoad to load a ui created by QtDesigner and everything works well, but I need to move some widgets at run-time inside the Script.
    I think that the best solution is to use the "setDefaultPrototype" when I create widgets(by QUiLoad), but since now I don't find the right way...

    If someone has a peace of code as example .....

    Thanks
    mpantoli

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Script add method

    I think you rather need to expose the top level widgets and access all the other widgets through the parent-child relationship. If the latter is not possible, traverse the widget tree and expose each of them separately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01
  2. Testing app with Qt Script
    By Angelo Moriconi in forum Qt Programming
    Replies: 5
    Last Post: 23rd October 2007, 08:20
  3. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  4. Replies: 4
    Last Post: 10th March 2007, 18:01
  5. variable in method not initialized?!
    By frosch in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2006, 14:09

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
  •  
Qt is a trademark of The Qt Company.