Results 1 to 5 of 5

Thread: How to cast QPointer<T> to QPointer<childT>

  1. #1
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question How to cast QPointer<T> to QPointer<childT>

    Hi there,

    i have a small problem while casting from QPointer<T> to QPointer<ChildT>

    I tried this.

    QPointer<T> tObject=new T();
    //QPointer<ChildT> childtObject=(QPointer<T>)tObjec;// this does not work;
    QPointer<ChildT> childtObject=(ChildT*)(T*)tObjec;// this work;
    CAFU......

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to cast QPointer<T> to QPointer<childT>

    try qobject_cast<T>
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to cast QPointer<T> to QPointer<childT>

    do you mean qobject_cast<childT> thanks for the fast answer
    CAFU......

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to cast QPointer<T> to QPointer<childT>

    Since QPointer is a "normal" pointer at all you also could use
    Qt Code:
    1. QPointer<T> tObject = new T();
    2. QPointer<ChildT> childtObject = (ChildT*) tObjec.data();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to cast QPointer<T> to QPointer<childT>

    to cast to the parentclass pointer and then to child class pointer is correct:


    QPointer<T> tObject=new T();
    QPointer<ChildT> childtObject=(ChildT*)(T*)tObjec;
    CAFU......

Similar Threads

  1. Problem using QPointer
    By weaver4 in forum Newbie
    Replies: 8
    Last Post: 20th February 2010, 05:05
  2. QMutableVectorIterator and QPointer?
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 19:39
  3. QPointer and thread safety
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2008, 09:22
  4. MetaPointer: QPointer with Signals and Slots
    By ntessore in forum Qt-based Software
    Replies: 2
    Last Post: 21st May 2008, 20:17
  5. QPointer and double deletion
    By mtrpoland in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:49

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.