Results 1 to 10 of 10

Thread: QPointer gives compilation errors.

  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPointer gives compilation errors.

    I have class B inheriting Class A which in turn inherits QObject.
    Qt Code:
    1. class A: public QObject{
    2. Q_OBJECT
    3. ...
    4. };
    5. class B: public A{
    6. Q_OBJECT
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 
    At some place I instantiate class B. At some other place I want to use Class B pointer. To avoid dangling pointer, QPointer looked very interesting. So while storing the pointer of class B, I used QPointer<B> bptr
    But bptr = bClassInstancePtr gives compilation errors like cannot convert from B* to QObject* . What am I doing wrong?

  2. #2
    Join Date
    Sep 2011
    Location
    Ukraine, Kharkov
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    What is type of bClassInstancePtr? Can you give a complete example with creation and assign pointer?

  3. #3
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    Let me explain it again. I intend to keep a pointer of class B in some class X. I have instantiated class B(new'ed class B) in class Y. From there i wish to pass the pointer of new'ed class B to class X. To avoid worrying about dangling pointer, instead of having B* bptr, I have QPointer<B> bptr.
    Qt Code:
    1. class A: public QObject{
    2. Q_OBJECT
    3. ...
    4. };
    5. class B: public A{
    6. Q_OBJECT
    7. ...
    8. };
    9.  
    10. class Y{
    11. Y(X* px){
    12. x=px;
    13. bptr = new B();
    14. someMethodToAssignInX()
    15.  
    16. }
    17. private:
    18. void someMethodToAssignInX(){x->bptr = bptr;}
    19. B* bptr;
    20. X* x;
    21. }
    22. class X{
    23. public:
    24. QPointer<B> bptr;
    25. }
    To copy to clipboard, switch view to plain text mode 

    Basically I want to use Qpointer to hold my pointer reference to class so that I can reap QPointer's benefits. I am not able to do so because of compilation errors.

  4. #4
    Join Date
    Sep 2011
    Location
    Ukraine, Kharkov
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    Can you write what exactly writes the compiler? Maybe you didn't write include of class B in assign place?

  5. #5
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    Compiler doesn't know how to convert from B* to QObject* . (!= No conversion from B* to QObject*)
    And why should i need to include the file with class B in the file where i write class X? I don't dereference B ptr ever. All i do is store the pointer. Why shouldn't a forward declaration do?

  6. #6
    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: QPointer gives compilation errors.

    Quote Originally Posted by pkj View Post
    What am I doing wrong?
    Add #include "b.h" to your code where you do the assignment.
    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.


  7. The following user says thank you to wysota for this useful post:

    pkj (6th September 2011)

  8. #7
    Join Date
    Sep 2011
    Location
    Ukraine, Kharkov
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    And why should i need to include the file with class B in the file where i write class X?
    QPointer uses mechanisms of QObject. He needed convert your type to QObject. I think, QPointer uses destroyed() signal from QObject.

  9. The following user says thank you to UASlaX for this useful post:

    pkj (6th September 2011)

  10. #8
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    #include "b.h" did the trick. Thanks. What I don't get, however, was why was it necessary. Going through qpointer code, I saw they store it as QObject*. Now QObject was being include'd. QPointer class is only interested in the QObject part of the class B. I think it should have gone through??

  11. #9
    Join Date
    Sep 2011
    Location
    Ukraine, Kharkov
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer gives compilation errors.

    Quote Originally Posted by pkj View Post
    #include "b.h" did the trick. Thanks. What I don't get, however, was why was it necessary. Going through qpointer code, I saw they store it as QObject*. Now QObject was being include'd. QPointer class is only interested in the QObject part of the class B. I think it should have gone through??
    As the compiler know that B is derived from QObject? You give to QPointer type B and QPointer converts them to QObject. For this conversion is necessary to know full specification of the type.

  12. #10
    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: QPointer gives compilation errors.

    Quote Originally Posted by UASlaX View Post
    QPointer uses mechanisms of QObject. He needed convert your type to QObject. I think, QPointer uses destroyed() signal from QObject.
    No, QPointer doesn't use destroyed(), there is a dedicated mechanism embedded in QObject for handling QPointer.

    Quote Originally Posted by pkj View Post
    What I don't get, however, was why was it necessary.
    Somewhere in your code you have the following line:
    Qt Code:
    1. class B;
    To copy to clipboard, switch view to plain text mode 
    which informs the compiler (forward declares the B class) that there exists a class called "B". It lets you use pointers to B in your code (as the size of pointer is fixed) without a full declaration of the B class (which is needed to use objects and methods as the compiler needs to know the size of the object and offset of methods in the object). But it doesn't inform the compiler that B is derived from QObject. Without that knowledge the compiler cannot allow a cast from B to QObject.
    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.


  13. The following user says thank you to wysota for this useful post:

    pkj (7th September 2011)

Similar Threads

  1. Qt 4.7.0 source compilation errors (MSVC 2010, Windows 7 Ultimate 64-bit)
    By andrey.khaletsky in forum Installation and Deployment
    Replies: 3
    Last Post: 30th September 2010, 08:16
  2. Replies: 0
    Last Post: 26th June 2010, 20:47
  3. compilation errors QT 4.6.2
    By loud91 in forum Installation and Deployment
    Replies: 2
    Last Post: 7th May 2010, 13:33
  4. How to cast QPointer<T> to QPointer<childT>
    By cafu in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2010, 10:51
  5. Qt compilation errors
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2006, 17:29

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.