Results 1 to 4 of 4

Thread: Handle QLineEdit inside script

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

    Default Handle QLineEdit inside script

    I use QScript in my app in such manner:

    I created object to pass it to script:
    Qt Code:
    1. class ScriptMediator :public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ScriptMediator(QWidget *parent = 0);
    6. void AddWidget(QWidget * child)
    7. {
    8. list.append(child);
    9. }
    10. private:
    11. QList<QWidget *> list;
    12. public slots:
    13. void okClicked();
    14. void cancelClicked();
    15. void print(QString string);
    16. QWidget * GetWidget(QString name);
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. p_mediator.AddWidget(ui->okButton); // QPushButton
    2. p_mediator.AddWidget(ui->cancelButton); // QPushButton
    3. p_mediator.AddWidget(ui->edit); // QLineEdit
    4.  
    5. QScriptValue objectValue = p_engine.newQObject(&p_mediator);
    6. p_engine.globalObject().setProperty("Mediator", objectValue);
    7. QScriptValue result = p_engine.evaluate(contents, fileName);
    To copy to clipboard, switch view to plain text mode 

    Script code:

    Qt Code:
    1. function okClicked()
    2. {
    3. Mediator.okClicked();
    4. }
    5. function cancelClicked()
    6. {
    7. Mediator.cancelClicked();
    8. }
    9. function returnPressed()
    10. {
    11. Mediator.print( this.text );
    12. }
    13.  
    14. Mediator.GetWidget("okButton").clicked.connect(okClicked);
    15. Mediator.GetWidget("cancelButton").clicked.connect(cancelClicked);
    16. Mediator.GetWidget("code").returnPressed.connect(returnPressed);
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    Last edited by pkj; 14th October 2013 at 06:43.

  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: Handle QLineEdit inside script

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

  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: 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
    Qt Code:
    1. function returnPressed()
    2. {
    3. Mediator.print( Mediator.GetWidget("code").text );
    4. }
    To copy to clipboard, switch view to plain text mode 
    and it works good

Similar Threads

  1. how to run a vbs script in resource from inside QT
    By sherifomran in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2013, 11:30
  2. Replies: 3
    Last Post: 27th January 2013, 13:28
  3. How to read value which written inside the QLineEdit
    By RENOLD in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2012, 13:59
  4. Replies: 5
    Last Post: 10th December 2010, 19:24
  5. Use a QAbstractTableModel inside a QML script
    By zuck in forum Qt Programming
    Replies: 0
    Last Post: 7th January 2010, 16:16

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.