Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Telus
Accepted Solution

Return exception from qobject_cast

I have a class which I reference with qobject_cast<myClass*>()

 

I'm also referencing a variable inside it qobject_cast<myClass*>()->myVariable

 

My issue is if myVariable is null, instead of letting me know it just crashes the application.  What I need is to know if the variable is null, so that I can create a new instance of the class which will then fill the variable and I can move on to the next task.

 

i've tried checking if it's null, or 0 and neither of those work.  Is what I'm attempting even possible?  or is there a better way I should be trying to do this.

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,090
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Return exception from qobject_cast

[ Edited ]

There should be no reason you can't check for an invalid pointer.

 

Post your code for both the instantiation of the class, the clas constructor and header and the code around the check.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
greenmr
Posts: 875
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Return exception from qobject_cast

That is exactly the behaviour I would expect for your second use-case, which is a very unsafe way to cast. qobject_cast doesn't throw an exception if the cast is not successful, it just returns NULL instead of a pointer. When you try to use a class variable or method directly from the qobject_cast you will crash your app if the cast is unsuccessful. You must break it up into two parts:

 

MyClass* ptr = qobject_cast<myClass*>();
if ( !ptr )
   ptr = new Myclass();

// ---Now it is safe to reference methods and variables even if the cast failed
ptr->myVariable;

 


slashkyle wrote:

I have a class which I reference with qobject_cast<myClass*>()

 

I'm also referencing a variable inside it qobject_cast<myClass*>()->myVariable

 

My issue is if myVariable is null, instead of letting me know it just crashes the application.  What I need is to know if the variable is null, so that I can create a new instance of the class which will then fill the variable and I can move on to the next task.

 

i've tried checking if it's null, or 0 and neither of those work.  Is what I'm attempting even possible?  or is there a better way I should be trying to do this.

 

 






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Telus

Re: Return exception from qobject_cast

That worked perfectly greenmr, Thank you =)

 

I saw that example when I googled about this, but I didn't catch on to assining a new myClass to the pointer if it wasn't already there.

 

I had kind of thought about trying something like that but figured it would fail because my goal was to create a new myClass later on, but now that it's working I see that later on all i have to do is call the pointer.

Please use plain text.
Developer
greenmr
Posts: 875
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Return exception from qobject_cast

[ Edited ]

qobject_cast is a Qt specific version of the standard C++ dynamic_cast. Both of them are designed to do the same thing, allow you to cast a pointer whose actual type isn't known until runtime. If you know for certain that your ptr is ALWAYS going to point to a MyClass or a descendent of it and thus will always convert properly, then you can use static_cast, but if there is any chance that your ptr might point to something else, then you have to use qobject_cast or  dynamic_cast. They are functionally identical and the only difference is that while dynamic_cast requires that the C++ compiler has RTTI support and it doesn't work across dynamic library boundaries, qobject_cast suffers from neither of those limitations and so can be used on every platform that Qt runs on. GCC on BlackBerry 10 does have RTTI support so in practice you can use either variant to dynamically cast pointers, but since discovering qobject_cast I use it exclusively in my Qt/Cascades apps in case I ever want to port them to another platform.

 

Both qobject_cast and dynamic_cast are very useful for determining exactly which descendent of a passed parameter type was actually passed:

 

MyClass::myFunc( QObject* object ) {
   Page* page = qobject_cast<Page*>( object );
   if ( page ) {
      // ---Passed parameter WAS a Page... do something with it
   } else {
      Container* container = qobject_cast<Container*>( object );
      if ( container ) {
         // ---Passed parameter WAS a Container... do something with it
      } else {
         Label* label = qobject_cast<Label*>( object );
         if ( label ) {
            // ---Passed parameter WAS a Label... do something with it
         } else {
            // ---Passed parameter type not handled by this function... report error
         }
      }
   }
}

 



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.