[Qt-interest] QtScript, passing a var as a QString to a slot
Nathan Carter
nathancarter5 at gmail.com
Mon Mar 9 15:33:02 CET 2009
One solution would be to set up your own connections from script code
to C++ code explicitly in a C++ routine. Then you have one central
location you would need to change if you intend to add a new scripting
environment. Qt is especially helpful with this because you can use
QMetaObject to loop through the functions in an object and decide
which ones to set up as callable from script based on their name,
signature, whatever. I've done this in one of my projects, but it's
not exactly like what you're doing. Here's the general idea (for
QtScript in particular):
void connectFunctionsFromAnObject ( QObject* theObject, QScriptEngine*
engine )
{
const QMetaObject* mo = metaObject();
for ( int i = mo->methodOffset() ; i <= mo->methodCount() ; i++ ) {
QMetaMethod m = mo->method( i );
QString name = m.signature();
name = name.left( name.indexOf( "(" ) );
// here's the condition I used to flag things I wanted connected,
but yours may differ:
if ( ( m.methodType() == QMetaMethod::Slot ) &&
name.startsWith( "script_" ) ) {
QScriptValue func = engine->newFunction( dispatcherFunction );
QScriptValue data = engine->newObject();
data.setProperty( "slotName", engine->newVariant( name ) );
// here I also stored in another property of data a code that
referred to theObject
// but that was specific to my application; yours will have to do
whatever works for you
func.setData( data );
scope.setProperty( name.mid( 7 ), func ); // strip off the script_
part
}
}
}
QScriptValue dispatcherFunction ( QScriptContext* context,
QScriptEngine* engine )
{
QScriptValue data = context->callee().data();
// then put here some code that fetches theObject and checks to see
if it has a function
// of the correct name and signature, etc., etc.
// if not, return context->throwError( ... );
QScriptValue result;
bool success = QMetaObject::invokeMethod( theObject, functionName,
Qt::DirectConnection,
Q_RETURN_ARG( QScriptValue, result ),
... other args ... );
// or instead of the above line, depending on your conventions, you
may need to inspect
// context to see how many arguments there are, and do an ugly case
statement,
// but it can be done, however messy :)
if ( !success )
return context->throwError( ... );
return result;
}
Nathan
On Mar 9, 2009, at 3:19 AM, Jaco Naude wrote:
> Hi Rohan, Alexandre
>
> Thanks for your replies. Both ideas sounds good. The problem is that
> I want to make "generic" functions. Functions that can be used in c+
> + code, Qt Script and also future script backends (Like PythonQt for
> example.).
>
> I guess I can create a function for each environment from where it
> is called but I am hoping to have a more generic solution. The
> downside of having different functions is that it will make usage
> more difficult. For example, in Qt Script environment you would need
> to call the QtScript function, say AddQt. In Python you would need
> to call AddPython or something like that. I guess the use of auto
> completion will make it easier by presenting only AddQt in QtScript
> environment, and AddPython in the Python environment. But still the
> best solution will be to have only 1 function.
>
> Thanks again, if there are any ways to do this, please let me know.
> Jaco Naude
>
> On Mon, Mar 9, 2009 at 1:45 AM, Rohan McGovern <rohan.mcgovern at nokia.com
> > wrote:
> On Friday 06 March 2009, ext Jaco Naude wrote:
> > Hi there
> >
> > I hope that someone can point me in the right direction here. I'm
> > new to scripting with QtScript and I'm having some problems. I
> > have the following two slots:
> >
> > Q_INVOKABLE bool MatrixAdd(const QString& variable_to_add, const
> > QString& new_name); Q_INVOKABLE bool MatrixSubtract(const
> > QString& variable_to_subtract, const QString& new_name, QString&
> > result_string);
> >
>
> Hi,
>
> You could consider rewriting these functions to return `void' and
> have them throw script exceptions on error. The exception could
> contain whatever information is interesting to you.
>
> --
> Rohan McGovern
> Qt Software, Nokia
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090309/a575b43c/attachment.html
More information about the Qt-interest-old
mailing list