Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to delete the objects from QMap?

How to delete the objects from QMap?

Scheduled Pinned Locked Moved Solved General and Desktop
qmapqdeletealldeletedeletelater
5 Posts 3 Posters 3.4k 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.
  • Y Offline
    Y Offline
    Yash001
    wrote on last edited by
    #1

    I would like to delete all object from the QMap. I try different ways but destructor is not call. what is wrong am i doing?

    try 1:

    #include <QtCore/QCoreApplication>
    #include "qalgorithms.h"
    
    #include "qdebug.h"
    #pragma pack(push,1)
    
    class absrectClass {
    public:
    	void fun() {
    		qDebug() << "fun is call";
    	}
    	virtual void fun1() = 0;
    protected:
    	int x;
    };
    
    class derived2 : public absrectClass {
    
    public:
    	~derived2() {
    		qDebug() << "delete derived2 object";
    	}
    
    	void fun1() {
    		qDebug() << "fun1 is call from derived2";
    	}
    
    };
    
    int main(int argc, char *argv[])
    {
    	QCoreApplication a(argc, argv);
    	QMap <QString,absrectClass*> mapper;
    	mapper["object1"] = new derived2();
    	mapper["object2"] = new derived2();
    	
    	qDeleteAll(mapper);
    
    

    try 2:

    #include <QtCore/QCoreApplication>
    #include "qalgorithms.h"
    
    #include "qdebug.h"
    #pragma pack(push,1)
    
    class absrectClass {
    public:
    	void fun() {
    		qDebug() << "fun is call";
    	}
    	virtual void fun1() = 0;
    protected:
    	int x;
    };
    
    class derived2 : public absrectClass {
    
    public:
    	~derived2() {
    		qDebug() << "delete derived2 object";
    	}
    
    	void fun1() {
    		qDebug() << "fun1 is call from derived2";
    	}
    
    };
    
    int main(int argc, char *argv[])
    {
    	QCoreApplication a(argc, argv);
    	QMap <QString,absrectClass*> mapper;
    	mapper["object1"] = new derived2();
    	mapper["object2"] = new derived2();
    	
    	foreach(auto it, mapper.values())
    	{
    		delete it;
    	}
    
    	return a.exec();
    }
    
    

    any one please give me direction?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You store pointers in the map. So if you call clear() it just deletes the pointers.
      Not the objects they points too.
      You can use
      void qDeleteAll(const Container &c)
      and
      clear() to completely delete all of it.
      http://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll

      Y 1 Reply Last reply
      3
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        I get this :

        warning: delete called on 'absrectClass' that is abstract but has non-virtual destructor [-Wdelete-non-virtual-dtor]
                delete it;
                ^
        

        Something's missing in your code :)

        Y 1 Reply Last reply
        2
        • mrjjM mrjj

          Hi
          You store pointers in the map. So if you call clear() it just deletes the pointers.
          Not the objects they points too.
          You can use
          void qDeleteAll(const Container &c)
          and
          clear() to completely delete all of it.
          http://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll

          Y Offline
          Y Offline
          Yash001
          wrote on last edited by
          #4

          @mrjj Thank you for response.

          You can see in "try 1" for deleting object I use qDeleteAll(mapper); inside the main function.

          I found mistake, I forgot to make virtual destructor of base class.

          virtual ~absrectClass() {
          	}
          

          Now it is call to derived class destructor .

          Thank you

          1 Reply Last reply
          1
          • M mpergand

            I get this :

            warning: delete called on 'absrectClass' that is abstract but has non-virtual destructor [-Wdelete-non-virtual-dtor]
                    delete it;
                    ^
            

            Something's missing in your code :)

            Y Offline
            Y Offline
            Yash001
            wrote on last edited by
            #5

            @mpergand Thank you for help.

            1 Reply Last reply
            0

            • Login

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