Results 1 to 17 of 17

Thread: Using QProcess in QtScript

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Posts
    15

    Default Using QProcess in QtScript

    Hello everyone,

    I'm trying to use QProcess from QtScript.

    I've tried a lot of things, but it seems the only way I can make it build is by using Q_SCRIPT_DECLARE_QMETAOBJECT(QProcess, QObject*), and then do:

    Qt Code:
    1. QScriptValue proc = engine.scriptValueFromQMetaObject<QProcess>();
    2. engine.globalObject().setProperty("Process", proc);
    To copy to clipboard, switch view to plain text mode 

    But then I do in the script:

    Qt Code:
    1. var n = new Process();
    2. n.start("someapp");
    To copy to clipboard, switch view to plain text mode 

    And it results in TypeError: Result of expression 'n.start' [undefined] is not a function.

    What am I doing wrong?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using QProcess in QtScript

    Easy, Process is a QObject

    You need to tell the script engine that Process is actually a QProcess.

    What you have is correct but not complete. Also use qScriptRegisterMetaType()

  3. #3
    Join Date
    Nov 2009
    Posts
    15

    Default Re: Using QProcess in QtScript

    Can you be more specific?
    It seems I need to specify a toScriptValue and fromScriptValue functions, but I'm not really sure how to do it.

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using QProcess in QtScript

    Something in the lines of:

    Qt Code:
    1. class MyObject : public QObject
    2. {
    3. Q_OBJECT
    4. ...
    5. };
    6.  
    7. Q_DECLARE_METATYPE(MyObject*)
    8.  
    9. QScriptValue myObjectToScriptValue(QScriptEngine *engine, MyObject* const &in)
    10. { return engine->newQObject(in); }
    11.  
    12. void myObjectFromScriptValue(const QScriptValue &object, MyObject* &out)
    13. { out = qobject_cast<MyObject*>(object.toQObject()); }
    14.  
    15. ...
    16.  
    17. qScriptRegisterMetaType(&engine, myObjectToScriptValue, myObjectFromScriptValue);
    To copy to clipboard, switch view to plain text mode 

    Edit: changed the code as mentioned on the Qt documentation website
    Last edited by tbscope; 5th November 2010 at 16:36.

  5. #5
    Join Date
    Nov 2009
    Posts
    15

    Default Re: Using QProcess in QtScript

    I had to change a bit the functions since QProcess can't be copied.

    But now I'm having this issue:
    Qt Code:
    1. /usr/include/qt4/QtCore/qmetatype.h: In static member function ‘static int QMetaTypeId2<T>::qt_metatype_id() [with T = QProcess]’:
    2. /usr/include/qt4/QtCore/qmetatype.h:230: instantiated from ‘int qMetaTypeId(T*) [with T = QProcess]’
    3. /usr/include/qt4/QtCore/qmetatype.h:243: instantiated from ‘int qRegisterMetaType(T*) [with T = QProcess]’
    4. /usr/include/qt4/QtScript/qscriptengine.h:402: instantiated from ‘int qScriptRegisterMetaType(QScriptEngine*, QScriptValue (*)(QScriptEngine*, const T&), void (*)(const QScriptValue&, T&), const QScriptValue&, T*) [with T = QProcess]’
    5. mainwindow.cpp:69: instantiated from here
    6. /usr/include/qt4/QtCore/qmetatype.h:169: error: ‘qt_metatype_id’ is not a member of ‘QMetaTypeId<QProcess>’
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

    Thanks again

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using QProcess in QtScript

    Yes, do not forget Q_DECLARE_METATYPE

  7. #7
    Join Date
    Nov 2009
    Posts
    15

    Default Re: Using QProcess in QtScript

    Right... I thought that was implicit since it's already a QObject...

    But now:
    Qt Code:
    1. /usr/include/qt4/QtCore/qprocess.h: In function ‘void* qMetaTypeConstructHelper(const T*) [with T = QProcess]’:
    2. /usr/include/qt4/QtCore/qmetatype.h:196: instantiated from ‘int qRegisterMetaType(const char*, T*) [with T = QProcess]’
    3. mainwindow.cpp:4: instantiated from here
    4. /usr/include/qt4/QtCore/qprocess.h:224: error: ‘QProcess::QProcess(const QProcess&)’ is private
    5. /usr/include/qt4/QtCore/qmetatype.h:142: error: within this context
    To copy to clipboard, switch view to plain text mode 

    this isn't as easy as it sounded... at least to me...

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using QProcess in QtScript

    :-)

    This isn't easy :-)

    Wrap your QProcess in a QSharedPointer.
    This whole QScript mess is very complex. On one hand you have to have copy constructors, but on the other hand, they are not allowed.
    You can solve this by using a QSharedPointer

  9. #9
    Join Date
    Nov 2009
    Posts
    15

    Default Re: Using QProcess in QtScript

    I'm going to come out as the stupidest guy in here but... how can I wrap it with an object that isn't a QObject?

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using QProcess in QtScript

    Note: I didn't compile this, so there might be a syntax error.

    Example:

    Qt Code:
    1. QScriptValue qScriptValueFromProcess(QScriptEngine *engine, const QSharedPointer<QProcess> &object)
    2. {
    3. return engine->newQObject(object.data());
    4. }
    5.  
    6. void qScriptValueToProcess(const QScriptValue & value, QSharedPointer<QProcess> & object)
    7. {
    8. }
    9.  
    10. Q_SCRIPT_DECLARE_QMETAOBJECT(QProcess, QObject*)
    11. Q_DECLARE_METATYPE(QSharedPointer<QProcess>)
    12.  
    13. QScriptValue processValue = m_engine->scriptValueFromQMetaObject<QProcess>();
    14. m_engine->globalObject().setProperty("Process", processValue);
    15.  
    16. qScriptRegisterMetaType(m_engine, qScriptValueFromProcess, qScriptValueToProcess);
    To copy to clipboard, switch view to plain text mode 

    m_engine is a member variable containing a pointer to a script engine.

  11. #11
    Join Date
    Nov 2009
    Posts
    15

    Default Re: Using QProcess in QtScript

    Great, so now it built, but it's the same problem as before, I can declare a variable, assign new Process(), but when I call variable.start("ls") it's the same error...

    Any ideas?

Similar Threads

  1. Replies: 0
    Last Post: 26th August 2010, 10:44
  2. QtScript
    By lamosca in forum Newbie
    Replies: 2
    Last Post: 20th May 2010, 23:01
  3. Replies: 0
    Last Post: 25th November 2009, 07:46
  4. Please help with QtScript
    By Haccel in forum Qt Programming
    Replies: 0
    Last Post: 16th December 2008, 05:59
  5. QtScript
    By QTInfinity in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2008, 20:10

Tags for this Thread

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.