Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6237257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:59:40+00:00 2026-05-24T10:59:40+00:00

In the function of itemChange, first ,I get the child item that will be

  • 0

In the function of itemChange, first ,I get the child item that will be added, then I use dynamic_cast cast it to ‘MyItem’, but the cast always fail.

 QVariant MyItem::itemChange ( GraphicsItemChange change, const QVariant & value )
{

if (change==ItemChildAddedChange)
{
  QGraphicsItem* item=value.value<QGraphicsItem*>();
if (item)
{
     MyItem* myItem=dynamic_cast<MyItem*>(item);//myItem always be NULL,
//although I know the item is 'MyItem' type.
     if (myItem)
      {
       qDebug()<<"successful!";
       }
       }
}
return QGraphicsItem::itemChange(change,value);
}

Thanks very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:59:41+00:00Added an answer on May 24, 2026 at 10:59 am

    Note the comment on itemChange:

    Note that the new child might not be fully constructed when this
    notification is sent; calling pure virtual functions on the child can
    lead to a crash.

    dynamic_cast can also fail if the object is not fully constructed. (I don’t quite understand the spec on this, but there are some cases where it will, some where it will not.) If you set the parent after constructing the item, it will work:

    #include <QtGui>
    
    class MyGraphicsItem : public QGraphicsRectItem {
    public:
      MyGraphicsItem(QGraphicsItem *parent, QGraphicsScene *scene)
        : QGraphicsRectItem(0.0, 0.0, 200.0, 200.0, parent, scene) {
        setBrush(QBrush(Qt::red));
      }
    protected:
      QVariant itemChange(GraphicsItemChange change, const QVariant &value) {
        if (change == QGraphicsItem::ItemChildAddedChange) {
          QGraphicsItem* item = value.value<QGraphicsItem*>();
          if (item) {
            MyGraphicsItem* my_item=dynamic_cast<MyGraphicsItem*>(item);
            if (my_item) {
              qDebug() << "successful!";
            }
          }
        }
        return QGraphicsRectItem::itemChange(change, value);
      }
    };
    
    int main(int argc, char **argv) {
      QApplication app(argc, argv);
    
      QGraphicsScene scene;
      MyGraphicsItem *item = new MyGraphicsItem(NULL, &scene);
    
      // This will work.
      MyGraphicsItem *item2 = new MyGraphicsItem(NULL, &scene);
      item2->setParentItem(item);
    
    //  // This will not work.
    //  MyGraphicsItem *item2 = new MyGraphicsItem(item, &scene);
    
      QGraphicsView view;
      view.setScene(&scene);
      view.show();
    
      return app.exec();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function strip_cdata($string) { preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches); return str_replace($matches[0], $matches[1], $string); } I use the
In this example: http://www.java2s.com/Code/Cpp/Qt/InteractwithQGraphicsItem.htm the itemChange() function is where all of the updating work
function getsession(){ var data = $.get('../session', function(data) { return data; }); return data; }
function CloseWindow() { window.close(); } The above code is working fine for IE but
function countJSONObjByKeyValue(obj, key, value) { var countJSONShows = 0; $.each(obj, function(i, item) { if(obj[i].key
Can I use template to create several instantiations of some function, different only in
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
Function FillAdminAccount() As Boolean FillAdminAccount = True Try SQLconn.ConnectionString = connect timeout=9999999; & _
function Submit_click() { if (!bValidateFields()) return; } function bValidateFields() { /// <summary>Validation rules</summary> ///
function main() { Hello(); } function Hello() { // How do you find out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.