[Qt-interest] QScript Newbie Question

Sean Harmer sean.harmer at maps-technology.com
Mon Feb 8 15:45:38 CET 2010


On Monday 08 February 2010 14:16:06 Alexander Carôt wrote:
> Hi Sean,
> 
> this was good hint ! I didn't get how you can access the line edit without
>  attaching it to a GUI widget, which is why I modified the code in such
>  way:

If the QLineEdit has no parent it just appears as a top-level window as in my 
simple example.

More usually you would create a QDialog, QMainWindow, or QWidget subclass and 
a .ui file (in designer) and create the QLineEdit as a child of your special 
dialog/main window/widget. You could then make the QScriptEngine object a 
child of that special widget too. For e.g.

class MyWidget : public QWidget
{
Q_OBJECT
public:
    MyWidget( QWidget* parent = 0 )
    : QWidget( parent ),
      ui( new Ui::MyWidget ),
      engine( new QScriptEngine( this ) )
    {
        ui->setupUi( this );
        handler = engine.evaluate( "(function myFunc( text ) { print('Text 
changed to:', text ); })" ); 
        qScriptConnect( ui->edit, SIGNAL( textChanged( const QString& ) ), 
QScriptValue(), handler );
        ...
    }

private:
    Ui::MyWIdget* ui;
    QScriptEngine* engine;
    QScriptValue handler;
};

Where the Ui::MyWidget class is created by the uic from your .ui file as part 
of the build process.

Then in your main function just create an instance of the MyWidget widget with 
no parent and show it.

I hope that makes sense.

Sean




More information about the Qt-interest-old mailing list