Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. delete only the child class

delete only the child class

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
9 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bozo6919
    wrote on 26 Jul 2018, 15:23 last edited by bozo6919
    #1

    Hi,

    I have a class named "ContentPanel" and in my programme, this class can be transformed into another children class like "Image", "TextEdit" or "TreeView".
    I want to delete only characteristics of the child class. I tried this but this code delete "ContentPanel" too :

    	if (content->content == tr("Text"))
    		delete dynamic_cast<TextEdit *>(content);
    	else if (content->content == tr("Image"))
    		delete dynamic_cast<Image *>(content);
    	else if (content->content == tr("TreeView"))
    		delete dynamic_cast<TreeView *>(content);
    

    When I called a child class for transformed the class "ContentPanel", I do this :

    Image::Image(QString path, ContentPanel *parent)
    {
    	// Some code
    
    	parent = this;
    }
    

    With that, I transform my class ContentPanel into a child class named "Image". And I want only delete Image, not ContentPanel. How can I do ?

    Sorry for my English ^^'

    J 1 Reply Last reply 26 Jul 2018, 17:01
    0
    • B Offline
      B Offline
      bozo6919
      wrote on 27 Jul 2018, 12:49 last edited by
      #9

      Ok thanks :)

      Sorry for my English ^^'

      1 Reply Last reply
      1
      • B bozo6919
        26 Jul 2018, 15:23

        Hi,

        I have a class named "ContentPanel" and in my programme, this class can be transformed into another children class like "Image", "TextEdit" or "TreeView".
        I want to delete only characteristics of the child class. I tried this but this code delete "ContentPanel" too :

        	if (content->content == tr("Text"))
        		delete dynamic_cast<TextEdit *>(content);
        	else if (content->content == tr("Image"))
        		delete dynamic_cast<Image *>(content);
        	else if (content->content == tr("TreeView"))
        		delete dynamic_cast<TreeView *>(content);
        

        When I called a child class for transformed the class "ContentPanel", I do this :

        Image::Image(QString path, ContentPanel *parent)
        {
        	// Some code
        
        	parent = this;
        }
        

        With that, I transform my class ContentPanel into a child class named "Image". And I want only delete Image, not ContentPanel. How can I do ?

        J Offline
        J Offline
        JonB
        wrote on 26 Jul 2018, 17:01 last edited by JonB
        #2

        @bozo6919
        First, it took me a while to figure what you meant by "girl". You might like to edit your question to change all occurrences of girl to child (and the girls to children) for others to understand better.

        Second I don't know what you're really trying to do here.

        Third:

        With that, I transform my class ContentPanel into a girl [child] class named "Image".

        Whatever you're trying to do here, setting parent = this; is not going to do anything/what you think it might do. parent is the name of a parameter to your function. That means it's a local variable, you setting it to something isn't going to have any effect. If you think it's setting Image::parent, it is not.

        Someone else may guess better than I what you want, but I think you just want your ContentPanel to have a QWidget *m_content member variable, you new that to your particular widget class instance, and you delete that to get rid of the widget but keep the ContentPanel otherwise the same. No "child classes" or "transforming" involved.

        There may also be a suitable Qt class to use in place of what you're calling your "ContentPanel", depending on just what you want to do with it.

        1 Reply Last reply
        6
        • B Offline
          B Offline
          bozo6919
          wrote on 26 Jul 2018, 19:56 last edited by
          #3

          Ok thanks a lot, you give me a lot of information :)
          I'll tried tomorrow to do an other things :)

          Sorry for my English ^^'

          J 1 Reply Last reply 27 Jul 2018, 04:55
          1
          • B bozo6919
            26 Jul 2018, 19:56

            Ok thanks a lot, you give me a lot of information :)
            I'll tried tomorrow to do an other things :)

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 27 Jul 2018, 04:55 last edited by
            #4

            @bozo6919 If you just want to delete there is no need to cast, simply

            delete content;
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply 27 Jul 2018, 10:08
            2
            • J jsulm
              27 Jul 2018, 04:55

              @bozo6919 If you just want to delete there is no need to cast, simply

              delete content;
              
              B Offline
              B Offline
              bozo6919
              wrote on 27 Jul 2018, 10:08 last edited by
              #5

              But if I do this, my class child will be deleted too?

              Sorry for my English ^^'

              J 1 Reply Last reply 27 Jul 2018, 10:20
              0
              • B bozo6919
                27 Jul 2018, 10:08

                But if I do this, my class child will be deleted too?

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 27 Jul 2018, 10:20 last edited by jsulm
                #6

                @bozo6919 What child class do you mean? How familiar are you with C++? If you delete an object in C++ then it is deleted. In Qt if you use parent-child relations then children are deleted as soon as parent is deleted.
                This

                if (content->content == tr("Text"))
                		delete dynamic_cast<TextEdit *>(content);
                	else if (content->content == tr("Image"))
                		delete dynamic_cast<Image *>(content);
                	else if (content->content == tr("TreeView"))
                		delete dynamic_cast<TreeView *>(content);
                

                is doing same as

                delete content;
                

                Or are you talking about polymorphism?

                class A
                {
                public:
                    virtual ~A() {...}
                };
                
                class B: public A
                {
                public:
                    ~B() {}  
                };
                
                A *object = new B();
                // This delete will call both ~B and ~A
                delete object;

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • B Offline
                  B Offline
                  bozo6919
                  wrote on 27 Jul 2018, 11:29 last edited by bozo6919
                  #7

                  They are some things who I'm not very familiar with. (for the moment ^^)

                  In your example, how I could do to delete only B? (and save attributes of A)

                  Sorry for my English ^^'

                  J 1 Reply Last reply 27 Jul 2018, 11:55
                  0
                  • B bozo6919
                    27 Jul 2018, 11:29

                    They are some things who I'm not very familiar with. (for the moment ^^)

                    In your example, how I could do to delete only B? (and save attributes of A)

                    J Online
                    J Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 27 Jul 2018, 11:55 last edited by jsulm
                    #8

                    @bozo6919 Sorry, what you are saying does not make sense.
                    If you delete an object it is deleted, completely. So:

                    A *object = new B();
                    // Here, object will be deleted completely. You can't delete only part of an object,
                    // either you delete the complete object or you don't delete anything.
                    // An object either exists or not, it cannot exist partially.
                    delete object;
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • B Offline
                      B Offline
                      bozo6919
                      wrote on 27 Jul 2018, 12:49 last edited by
                      #9

                      Ok thanks :)

                      Sorry for my English ^^'

                      1 Reply Last reply
                      1

                      6/9

                      27 Jul 2018, 10:20

                      • Login

                      • Login or register to search.
                      6 out of 9
                      • First post
                        6/9
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved