Results 1 to 4 of 4

Thread: Handle QLineEdit inside script

Hybrid View

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

    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
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

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

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

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