Results 1 to 4 of 4

Thread: Pointer in Q_PROPERTY

  1. #1
    Join Date
    Sep 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Pointer in Q_PROPERTY

    Hello,

    I have a problem accessing pointer property, defined as Q_PROPERTY, in JavaScript using QWebKit - here is a simple example:

    Qt Code:
    1. // MyObject.h
    2. class MyObject : public QObject {
    3. Q_OBJECT
    4.  
    5. public:
    6. Q_PROPERTY (QString* version READ getVersion)
    7.  
    8. QString* getVersion();
    9.  
    10. QString* version;
    11.  
    12. public slots:
    13. void populateJavaScriptWindowObject();
    14. };
    15.  
    16. // MyObject.cpp
    17. MyObject::MyObject() {
    18. qRegisterMetaType<QString*>("QString*");
    19.  
    20. version = new QString("1.0");
    21.  
    22. connect(app->getWebView()->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(populateJavaScriptWindowObject()));
    23. }
    24.  
    25. QString* MyObject::getVersion() {
    26. return version;
    27. }
    28.  
    29. void MyObject::populateJavaScriptWindowObject() {
    30. app->getWebView()->page()->mainFrame()->addToJavaScriptWindowObject("my", this);
    31. }
    To copy to clipboard, switch view to plain text mode 

    In JavaScript I should get following object:

    Qt Code:
    1. my: {version: "1.0"}
    To copy to clipboard, switch view to plain text mode 

    but instead I get:

    Qt Code:
    1. my: {version: ""}
    To copy to clipboard, switch view to plain text mode 

    I will be grateful for any help, thanks!

    Regards,
    Piotr
    Last edited by pkorzeniewski; 18th September 2012 at 23:25.

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

  3. #3
    Join Date
    Sep 2012
    Location
    Kharkiv
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Pointer in Q_PROPERTY

    It's better to expose QString object here because it is a standard QtScript type and will it be automatically converted to JS string.
    In this case qRegisterMetaType won't be needed.
    Qt Code:
    1. QString language();
    2. Q_PROPERTY(QString language READ language SCRIPTABLE true);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QString mLanguage;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QString MediaComponent::language()
    2. {
    3. return mLanguage;
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pointer in Q_PROPERTY

    ... And it won't create memory leaks.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Q_property
    By micky in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2012, 18:51
  2. Replies: 1
    Last Post: 4th December 2010, 17:20
  3. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  4. Why and when to use Q_PROPERTY
    By Vanir in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2007, 09:25
  5. How to Use Q_PROPERTY
    By mitesh_modi in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2006, 14:49

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.