How to down-cast QGraphicsItem to a created class when I have pointer attributs allready initialized?When is an integer<->pointer cast actually correct?Proper behavior when overloading casts to have class behave identically to pointersCasting pointer-to base class into a pointer-to derived classHow many levels of pointers can we have?How to cast/convert pointer to reference in C++Casting initializers down to a pointerCasting Class Pointer to Void PointerWhat is the difference between up-casting and down-casting with respect to class variableMethod pointer casting for any classboundingRectangle initialization of QGraphicsItem class

Numerical minimum of a one-valued function

Travel to USA with a stuffed puppet

Draw the ☣ (Biohazard Symbol)

Did the US Climate Reference Network Show No New Warming Since 2005 in the US?

Is a paralyzed creature limp or rigid?

Is directly echoing the user agent in PHP a security hole?

If magnetic force can't do any work, then how can we define a potential?

Can doublestrike kill a creature with totem armor?

Life post thesis submission is terrifying - Help!

If I have an accident, should I file a claim with my car insurance company?

Dissuading my girlfriend from a scam

Why there is no wireless switch?

Was "The Hobbit" ever abridged?

Why does the UK Prime Minister need the permission of Parliament to call a general election?

How can I describe hit point damage without talking about wounds?

ASCII Maze Rendering 3000

Is the interior of a Bag of Holding actually an extradimensional space?

Is there any reason to change the ISO manually?

Has Rey's new lightsaber been seen before in canon or legends?

Global variables and information security

Does an antenna tuner remove standing waves from a transmission line?

Why don't they build airplanes from 3D printer plastic?

Is the Levitate spell supposed to basically disable a melee-based enemy?

How does the UK House of Commons think they can prolong the deadline of Brexit?



How to down-cast QGraphicsItem to a created class when I have pointer attributs allready initialized?


When is an integer<->pointer cast actually correct?Proper behavior when overloading casts to have class behave identically to pointersCasting pointer-to base class into a pointer-to derived classHow many levels of pointers can we have?How to cast/convert pointer to reference in C++Casting initializers down to a pointerCasting Class Pointer to Void PointerWhat is the difference between up-casting and down-casting with respect to class variableMethod pointer casting for any classboundingRectangle initialization of QGraphicsItem class






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have my own QGraphicsPixmapItem which consists of a QGraphicsPixmapItem and some additional attributes (like std::string name)



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem 
private:
std::string name;
...
public:
MyQGraphicsPixmapItem();
explicit MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent = nullptr);
std::string getname() const;
...
;


And the constructor is like this :



MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent), name(name)
...



Here is the problem : I have a bunch of MyQGraphicsPixmapItem that I add in a QGraphicsScene. But when I use the method QGraphicsScene::itemAt(const QPointF &position, const QTransform &deviceTransform) const, it returns QGraphicsItem* (not MyQGraphicsPixmapItem*). So I suppose that I have to use a down-casting right ? But even after using the down casting like this :



 MyQGraphicsPixmapItem* item = static_cast<MyQGraphicsPixmapItem*>(QGraphicsScene::itemAt(...));
std::cout << item->getName() << std::endl;


it returns an empty string (like there was no this.name = name; as shown in the constructor).



In conclusion, I create a bunch of MyQGraphicsPixmapItem in a QGraphicsScene with the right name initialization (I tested it with std::cout during the creation of QGraphicsScene) but when I want to randomly select a QGraphicsItem and check what is his name, I use that QGraphicsScene::itemAt that sets back every time the std::string name to empty despite the down-casting.
Also, I'm extremely sure that I'm pointing at the right MyQGraphicsPixmapItem with the right arguments (I've done some tests).
I was also thinking about implementing the right "itemAt" in my class "MyScene" (which inherits, you guessed it, from "QGraphicsScene") but I would use type_casting again.



PS : Tell me if my question is well asked.



Sincerely yours










share|improve this question





















  • 2





    In general: static_cast may be slightly faster but this is because it won't be checked, dynamic_cast is preferable, and perhaps even more preferable is doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

    – user9088793
    Mar 28 at 5:22












  • I tried the dynamic and the QGraphicsItem casting and it still doesn't work. I've updated my question by making it simpler. Pleas check it out and tell me if you find something new.

    – A. Neo
    Mar 29 at 2:44











  • I'm a bit puzzled by this.name = name. Firstly why don't you assign it in the initializer list? Secondly... this is a pointer, so I would expect this->name.

    – user9088793
    Mar 29 at 7:08












  • 1. What do you mean by the initializer list ? The attributes are supposed to be assigned in the constructor. 2. I corrected the mistake.

    – A. Neo
    Mar 29 at 9:12












  • The initializer list is introduced by :. You can do MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name) Please see isocpp.org/wiki/faq/ctors#init-lists

    – user9088793
    Mar 29 at 9:15


















1















I have my own QGraphicsPixmapItem which consists of a QGraphicsPixmapItem and some additional attributes (like std::string name)



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem 
private:
std::string name;
...
public:
MyQGraphicsPixmapItem();
explicit MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent = nullptr);
std::string getname() const;
...
;


And the constructor is like this :



MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent), name(name)
...



Here is the problem : I have a bunch of MyQGraphicsPixmapItem that I add in a QGraphicsScene. But when I use the method QGraphicsScene::itemAt(const QPointF &position, const QTransform &deviceTransform) const, it returns QGraphicsItem* (not MyQGraphicsPixmapItem*). So I suppose that I have to use a down-casting right ? But even after using the down casting like this :



 MyQGraphicsPixmapItem* item = static_cast<MyQGraphicsPixmapItem*>(QGraphicsScene::itemAt(...));
std::cout << item->getName() << std::endl;


it returns an empty string (like there was no this.name = name; as shown in the constructor).



In conclusion, I create a bunch of MyQGraphicsPixmapItem in a QGraphicsScene with the right name initialization (I tested it with std::cout during the creation of QGraphicsScene) but when I want to randomly select a QGraphicsItem and check what is his name, I use that QGraphicsScene::itemAt that sets back every time the std::string name to empty despite the down-casting.
Also, I'm extremely sure that I'm pointing at the right MyQGraphicsPixmapItem with the right arguments (I've done some tests).
I was also thinking about implementing the right "itemAt" in my class "MyScene" (which inherits, you guessed it, from "QGraphicsScene") but I would use type_casting again.



PS : Tell me if my question is well asked.



Sincerely yours










share|improve this question





















  • 2





    In general: static_cast may be slightly faster but this is because it won't be checked, dynamic_cast is preferable, and perhaps even more preferable is doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

    – user9088793
    Mar 28 at 5:22












  • I tried the dynamic and the QGraphicsItem casting and it still doesn't work. I've updated my question by making it simpler. Pleas check it out and tell me if you find something new.

    – A. Neo
    Mar 29 at 2:44











  • I'm a bit puzzled by this.name = name. Firstly why don't you assign it in the initializer list? Secondly... this is a pointer, so I would expect this->name.

    – user9088793
    Mar 29 at 7:08












  • 1. What do you mean by the initializer list ? The attributes are supposed to be assigned in the constructor. 2. I corrected the mistake.

    – A. Neo
    Mar 29 at 9:12












  • The initializer list is introduced by :. You can do MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name) Please see isocpp.org/wiki/faq/ctors#init-lists

    – user9088793
    Mar 29 at 9:15














1












1








1








I have my own QGraphicsPixmapItem which consists of a QGraphicsPixmapItem and some additional attributes (like std::string name)



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem 
private:
std::string name;
...
public:
MyQGraphicsPixmapItem();
explicit MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent = nullptr);
std::string getname() const;
...
;


And the constructor is like this :



MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent), name(name)
...



Here is the problem : I have a bunch of MyQGraphicsPixmapItem that I add in a QGraphicsScene. But when I use the method QGraphicsScene::itemAt(const QPointF &position, const QTransform &deviceTransform) const, it returns QGraphicsItem* (not MyQGraphicsPixmapItem*). So I suppose that I have to use a down-casting right ? But even after using the down casting like this :



 MyQGraphicsPixmapItem* item = static_cast<MyQGraphicsPixmapItem*>(QGraphicsScene::itemAt(...));
std::cout << item->getName() << std::endl;


it returns an empty string (like there was no this.name = name; as shown in the constructor).



In conclusion, I create a bunch of MyQGraphicsPixmapItem in a QGraphicsScene with the right name initialization (I tested it with std::cout during the creation of QGraphicsScene) but when I want to randomly select a QGraphicsItem and check what is his name, I use that QGraphicsScene::itemAt that sets back every time the std::string name to empty despite the down-casting.
Also, I'm extremely sure that I'm pointing at the right MyQGraphicsPixmapItem with the right arguments (I've done some tests).
I was also thinking about implementing the right "itemAt" in my class "MyScene" (which inherits, you guessed it, from "QGraphicsScene") but I would use type_casting again.



PS : Tell me if my question is well asked.



Sincerely yours










share|improve this question
















I have my own QGraphicsPixmapItem which consists of a QGraphicsPixmapItem and some additional attributes (like std::string name)



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem 
private:
std::string name;
...
public:
MyQGraphicsPixmapItem();
explicit MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent = nullptr);
std::string getname() const;
...
;


And the constructor is like this :



MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent), name(name)
...



Here is the problem : I have a bunch of MyQGraphicsPixmapItem that I add in a QGraphicsScene. But when I use the method QGraphicsScene::itemAt(const QPointF &position, const QTransform &deviceTransform) const, it returns QGraphicsItem* (not MyQGraphicsPixmapItem*). So I suppose that I have to use a down-casting right ? But even after using the down casting like this :



 MyQGraphicsPixmapItem* item = static_cast<MyQGraphicsPixmapItem*>(QGraphicsScene::itemAt(...));
std::cout << item->getName() << std::endl;


it returns an empty string (like there was no this.name = name; as shown in the constructor).



In conclusion, I create a bunch of MyQGraphicsPixmapItem in a QGraphicsScene with the right name initialization (I tested it with std::cout during the creation of QGraphicsScene) but when I want to randomly select a QGraphicsItem and check what is his name, I use that QGraphicsScene::itemAt that sets back every time the std::string name to empty despite the down-casting.
Also, I'm extremely sure that I'm pointing at the right MyQGraphicsPixmapItem with the right arguments (I've done some tests).
I was also thinking about implementing the right "itemAt" in my class "MyScene" (which inherits, you guessed it, from "QGraphicsScene") but I would use type_casting again.



PS : Tell me if my question is well asked.



Sincerely yours







c++ qt casting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 11:04







A. Neo

















asked Mar 28 at 3:45









A. NeoA. Neo

488 bronze badges




488 bronze badges










  • 2





    In general: static_cast may be slightly faster but this is because it won't be checked, dynamic_cast is preferable, and perhaps even more preferable is doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

    – user9088793
    Mar 28 at 5:22












  • I tried the dynamic and the QGraphicsItem casting and it still doesn't work. I've updated my question by making it simpler. Pleas check it out and tell me if you find something new.

    – A. Neo
    Mar 29 at 2:44











  • I'm a bit puzzled by this.name = name. Firstly why don't you assign it in the initializer list? Secondly... this is a pointer, so I would expect this->name.

    – user9088793
    Mar 29 at 7:08












  • 1. What do you mean by the initializer list ? The attributes are supposed to be assigned in the constructor. 2. I corrected the mistake.

    – A. Neo
    Mar 29 at 9:12












  • The initializer list is introduced by :. You can do MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name) Please see isocpp.org/wiki/faq/ctors#init-lists

    – user9088793
    Mar 29 at 9:15













  • 2





    In general: static_cast may be slightly faster but this is because it won't be checked, dynamic_cast is preferable, and perhaps even more preferable is doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

    – user9088793
    Mar 28 at 5:22












  • I tried the dynamic and the QGraphicsItem casting and it still doesn't work. I've updated my question by making it simpler. Pleas check it out and tell me if you find something new.

    – A. Neo
    Mar 29 at 2:44











  • I'm a bit puzzled by this.name = name. Firstly why don't you assign it in the initializer list? Secondly... this is a pointer, so I would expect this->name.

    – user9088793
    Mar 29 at 7:08












  • 1. What do you mean by the initializer list ? The attributes are supposed to be assigned in the constructor. 2. I corrected the mistake.

    – A. Neo
    Mar 29 at 9:12












  • The initializer list is introduced by :. You can do MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name) Please see isocpp.org/wiki/faq/ctors#init-lists

    – user9088793
    Mar 29 at 9:15








2




2





In general: static_cast may be slightly faster but this is because it won't be checked, dynamic_cast is preferable, and perhaps even more preferable is doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

– user9088793
Mar 28 at 5:22






In general: static_cast may be slightly faster but this is because it won't be checked, dynamic_cast is preferable, and perhaps even more preferable is doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

– user9088793
Mar 28 at 5:22














I tried the dynamic and the QGraphicsItem casting and it still doesn't work. I've updated my question by making it simpler. Pleas check it out and tell me if you find something new.

– A. Neo
Mar 29 at 2:44





I tried the dynamic and the QGraphicsItem casting and it still doesn't work. I've updated my question by making it simpler. Pleas check it out and tell me if you find something new.

– A. Neo
Mar 29 at 2:44













I'm a bit puzzled by this.name = name. Firstly why don't you assign it in the initializer list? Secondly... this is a pointer, so I would expect this->name.

– user9088793
Mar 29 at 7:08






I'm a bit puzzled by this.name = name. Firstly why don't you assign it in the initializer list? Secondly... this is a pointer, so I would expect this->name.

– user9088793
Mar 29 at 7:08














1. What do you mean by the initializer list ? The attributes are supposed to be assigned in the constructor. 2. I corrected the mistake.

– A. Neo
Mar 29 at 9:12






1. What do you mean by the initializer list ? The attributes are supposed to be assigned in the constructor. 2. I corrected the mistake.

– A. Neo
Mar 29 at 9:12














The initializer list is introduced by :. You can do MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name) Please see isocpp.org/wiki/faq/ctors#init-lists

– user9088793
Mar 29 at 9:15






The initializer list is introduced by :. You can do MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name) Please see isocpp.org/wiki/faq/ctors#init-lists

– user9088793
Mar 29 at 9:15













1 Answer
1






active

oldest

votes


















1
















You should be able to use dynamic_cast but Qt provides also its own cast qgraphicsitem_cast which returns item casted to the given type if item is of that type or otherwise 0.



Note from the doc:




To make this function work correctly with custom items, reimplement
the type() function for each custom QGraphicsItem subclass.




Example class:



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem

public:
...
enum Type = UserType + 1 ;
int type() const override return Type;
...
;


Example test:



MyQGraphicsPixmapItem myItem;
qDebug() << &myItem;
QGraphicsItem *item = &myItem;
MyQGraphicsPixmapItem *castedItem = qgraphicsitem_cast<MyQGraphicsPixmapItem*>(item);
if (castedItem)
qDebug() << castedItem;
else
qWarning() << "casting failed";



Update:



QGraphicsScene::itemAt returns the topmost visible item at the specified position, or 0 if there are no items at this position.



If you verified with qgraphicsitem_cast that you successfully casted i.e. it returned pointer and not 0 then you really received your custom item and not some other graphics item. Then it should have name defined and not an empty string if you defined a name for all of your custom items.



To debug this issue further you could use QGraphicsScene::items to list all items on the scene considered visible by QGraphicsScene::itemAt. Do casting in the loop and print out a name of all of your custom items.



One thing that comes to my mind is that this is related to coordinates you are using when calling itemAt. You are e.g. doing your casting when you click with a mouse and then query the scene in the item coordinates and not with the scene coordinates. QGraphicsSceneMouseEvent::scenePos returns the mouse cursor position in scene coordinates. Maybe you are not getting the custom item you think?






share|improve this answer



























  • I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

    – A. Neo
    Mar 29 at 2:40











  • @A.Neo Please, check the update part of my answer.

    – talamaki
    Apr 1 at 5:37












  • Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

    – A. Neo
    Apr 2 at 13:04











  • @A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

    – talamaki
    Apr 2 at 13:26










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389865%2fhow-to-down-cast-qgraphicsitem-to-a-created-class-when-i-have-pointer-attributs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1
















You should be able to use dynamic_cast but Qt provides also its own cast qgraphicsitem_cast which returns item casted to the given type if item is of that type or otherwise 0.



Note from the doc:




To make this function work correctly with custom items, reimplement
the type() function for each custom QGraphicsItem subclass.




Example class:



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem

public:
...
enum Type = UserType + 1 ;
int type() const override return Type;
...
;


Example test:



MyQGraphicsPixmapItem myItem;
qDebug() << &myItem;
QGraphicsItem *item = &myItem;
MyQGraphicsPixmapItem *castedItem = qgraphicsitem_cast<MyQGraphicsPixmapItem*>(item);
if (castedItem)
qDebug() << castedItem;
else
qWarning() << "casting failed";



Update:



QGraphicsScene::itemAt returns the topmost visible item at the specified position, or 0 if there are no items at this position.



If you verified with qgraphicsitem_cast that you successfully casted i.e. it returned pointer and not 0 then you really received your custom item and not some other graphics item. Then it should have name defined and not an empty string if you defined a name for all of your custom items.



To debug this issue further you could use QGraphicsScene::items to list all items on the scene considered visible by QGraphicsScene::itemAt. Do casting in the loop and print out a name of all of your custom items.



One thing that comes to my mind is that this is related to coordinates you are using when calling itemAt. You are e.g. doing your casting when you click with a mouse and then query the scene in the item coordinates and not with the scene coordinates. QGraphicsSceneMouseEvent::scenePos returns the mouse cursor position in scene coordinates. Maybe you are not getting the custom item you think?






share|improve this answer



























  • I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

    – A. Neo
    Mar 29 at 2:40











  • @A.Neo Please, check the update part of my answer.

    – talamaki
    Apr 1 at 5:37












  • Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

    – A. Neo
    Apr 2 at 13:04











  • @A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

    – talamaki
    Apr 2 at 13:26















1
















You should be able to use dynamic_cast but Qt provides also its own cast qgraphicsitem_cast which returns item casted to the given type if item is of that type or otherwise 0.



Note from the doc:




To make this function work correctly with custom items, reimplement
the type() function for each custom QGraphicsItem subclass.




Example class:



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem

public:
...
enum Type = UserType + 1 ;
int type() const override return Type;
...
;


Example test:



MyQGraphicsPixmapItem myItem;
qDebug() << &myItem;
QGraphicsItem *item = &myItem;
MyQGraphicsPixmapItem *castedItem = qgraphicsitem_cast<MyQGraphicsPixmapItem*>(item);
if (castedItem)
qDebug() << castedItem;
else
qWarning() << "casting failed";



Update:



QGraphicsScene::itemAt returns the topmost visible item at the specified position, or 0 if there are no items at this position.



If you verified with qgraphicsitem_cast that you successfully casted i.e. it returned pointer and not 0 then you really received your custom item and not some other graphics item. Then it should have name defined and not an empty string if you defined a name for all of your custom items.



To debug this issue further you could use QGraphicsScene::items to list all items on the scene considered visible by QGraphicsScene::itemAt. Do casting in the loop and print out a name of all of your custom items.



One thing that comes to my mind is that this is related to coordinates you are using when calling itemAt. You are e.g. doing your casting when you click with a mouse and then query the scene in the item coordinates and not with the scene coordinates. QGraphicsSceneMouseEvent::scenePos returns the mouse cursor position in scene coordinates. Maybe you are not getting the custom item you think?






share|improve this answer



























  • I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

    – A. Neo
    Mar 29 at 2:40











  • @A.Neo Please, check the update part of my answer.

    – talamaki
    Apr 1 at 5:37












  • Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

    – A. Neo
    Apr 2 at 13:04











  • @A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

    – talamaki
    Apr 2 at 13:26













1














1










1









You should be able to use dynamic_cast but Qt provides also its own cast qgraphicsitem_cast which returns item casted to the given type if item is of that type or otherwise 0.



Note from the doc:




To make this function work correctly with custom items, reimplement
the type() function for each custom QGraphicsItem subclass.




Example class:



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem

public:
...
enum Type = UserType + 1 ;
int type() const override return Type;
...
;


Example test:



MyQGraphicsPixmapItem myItem;
qDebug() << &myItem;
QGraphicsItem *item = &myItem;
MyQGraphicsPixmapItem *castedItem = qgraphicsitem_cast<MyQGraphicsPixmapItem*>(item);
if (castedItem)
qDebug() << castedItem;
else
qWarning() << "casting failed";



Update:



QGraphicsScene::itemAt returns the topmost visible item at the specified position, or 0 if there are no items at this position.



If you verified with qgraphicsitem_cast that you successfully casted i.e. it returned pointer and not 0 then you really received your custom item and not some other graphics item. Then it should have name defined and not an empty string if you defined a name for all of your custom items.



To debug this issue further you could use QGraphicsScene::items to list all items on the scene considered visible by QGraphicsScene::itemAt. Do casting in the loop and print out a name of all of your custom items.



One thing that comes to my mind is that this is related to coordinates you are using when calling itemAt. You are e.g. doing your casting when you click with a mouse and then query the scene in the item coordinates and not with the scene coordinates. QGraphicsSceneMouseEvent::scenePos returns the mouse cursor position in scene coordinates. Maybe you are not getting the custom item you think?






share|improve this answer















You should be able to use dynamic_cast but Qt provides also its own cast qgraphicsitem_cast which returns item casted to the given type if item is of that type or otherwise 0.



Note from the doc:




To make this function work correctly with custom items, reimplement
the type() function for each custom QGraphicsItem subclass.




Example class:



class MyQGraphicsPixmapItem : public QGraphicsPixmapItem

public:
...
enum Type = UserType + 1 ;
int type() const override return Type;
...
;


Example test:



MyQGraphicsPixmapItem myItem;
qDebug() << &myItem;
QGraphicsItem *item = &myItem;
MyQGraphicsPixmapItem *castedItem = qgraphicsitem_cast<MyQGraphicsPixmapItem*>(item);
if (castedItem)
qDebug() << castedItem;
else
qWarning() << "casting failed";



Update:



QGraphicsScene::itemAt returns the topmost visible item at the specified position, or 0 if there are no items at this position.



If you verified with qgraphicsitem_cast that you successfully casted i.e. it returned pointer and not 0 then you really received your custom item and not some other graphics item. Then it should have name defined and not an empty string if you defined a name for all of your custom items.



To debug this issue further you could use QGraphicsScene::items to list all items on the scene considered visible by QGraphicsScene::itemAt. Do casting in the loop and print out a name of all of your custom items.



One thing that comes to my mind is that this is related to coordinates you are using when calling itemAt. You are e.g. doing your casting when you click with a mouse and then query the scene in the item coordinates and not with the scene coordinates. QGraphicsSceneMouseEvent::scenePos returns the mouse cursor position in scene coordinates. Maybe you are not getting the custom item you think?







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 29 at 7:36

























answered Mar 28 at 7:26









talamakitalamaki

3,2221 gold badge17 silver badges31 bronze badges




3,2221 gold badge17 silver badges31 bronze badges















  • I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

    – A. Neo
    Mar 29 at 2:40











  • @A.Neo Please, check the update part of my answer.

    – talamaki
    Apr 1 at 5:37












  • Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

    – A. Neo
    Apr 2 at 13:04











  • @A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

    – talamaki
    Apr 2 at 13:26

















  • I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

    – A. Neo
    Mar 29 at 2:40











  • @A.Neo Please, check the update part of my answer.

    – talamaki
    Apr 1 at 5:37












  • Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

    – A. Neo
    Apr 2 at 13:04











  • @A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

    – talamaki
    Apr 2 at 13:26
















I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

– A. Neo
Mar 29 at 2:40





I used your type_casting (including the dynamic one) and it still doesn't work. I also updated the question by making it simpler to understand. Please check it out.

– A. Neo
Mar 29 at 2:40













@A.Neo Please, check the update part of my answer.

– talamaki
Apr 1 at 5:37






@A.Neo Please, check the update part of my answer.

– talamaki
Apr 1 at 5:37














Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

– A. Neo
Apr 2 at 13:04





Dear god, something triggered in me when I read "the topmost visible item at the specified position". I actually had a cursor that moves right after I click on the square of my map. That's why the string was empty, the cursor never had his name assigned. I did the last test with the ScenePos (which told me there was no problem). I also printed only the items that had the name assigned (which told me that they exists) and that's why I was confused. Thanks for the help.

– A. Neo
Apr 2 at 13:04













@A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

– talamaki
Apr 2 at 13:26





@A.Neo I was assuming something like that might be a reason. That's why I wrote Then it should have name defined and not an empty string if you defined a name for all of your custom items. Great to hear you got it solved.

– talamaki
Apr 2 at 13:26








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389865%2fhow-to-down-cast-qgraphicsitem-to-a-created-class-when-i-have-pointer-attributs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript