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 free the memory usage of a qwidget?
Forum Updated to NodeBB v4.3 + New Features

How to free the memory usage of a qwidget?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.3k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by
    #1

    I have a simple example

    #ifndef TXTEDITOR_H
    #define TXTEDITOR_H
    
    #include <QWidget>
    
    namespace Ui {
    class TxtEditor;
    }
    
    class TxtEditor : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit TxtEditor(QWidget *parent = 0, QString file = "");
        ~TxtEditor();
    
    private:
        Ui::TxtEditor *ui;
    };
    
    #endif // TXTEDITOR_H
    
    #include "txteditor.h"
    #include "ui_txteditor.h"
    #include <QPlainTextEdit>
    #include <QTextStream>
    
    
    TxtEditor::TxtEditor(QWidget *parent, QString file) :
        QWidget(parent),
        ui(new Ui::TxtEditor)
    {
        ui->setupUi(this);
        QPlainTextEdit *editor = ui->plainTextEdit;
        QFile f(file);
        if(f.open(QIODevice::ReadOnly|QIODevice::Text)){
            QTextStream  in(&f);
            editor->setPlainText(in.readAll());
        }
    }
    
    TxtEditor::~TxtEditor()
    {
        delete ui;
    }
    
    

    I use it as

    TxtEditor *editor = new TxtEditor(NULL,"/boot/System.map-4.3.3");
        editor->show();
    

    I want to free all the memory used by editor when I close the editor window.
    How to do that?

    ValentinMicheletV 1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr Pang
      wrote on last edited by
      #2

      overload

      void TxtEditor::closeEvent(QCloseEvent *)
      {
          deleteLater();
      }
      

      Is this OK?

      M 1 Reply Last reply
      0
      • M Mr Pang

        I have a simple example

        #ifndef TXTEDITOR_H
        #define TXTEDITOR_H
        
        #include <QWidget>
        
        namespace Ui {
        class TxtEditor;
        }
        
        class TxtEditor : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit TxtEditor(QWidget *parent = 0, QString file = "");
            ~TxtEditor();
        
        private:
            Ui::TxtEditor *ui;
        };
        
        #endif // TXTEDITOR_H
        
        #include "txteditor.h"
        #include "ui_txteditor.h"
        #include <QPlainTextEdit>
        #include <QTextStream>
        
        
        TxtEditor::TxtEditor(QWidget *parent, QString file) :
            QWidget(parent),
            ui(new Ui::TxtEditor)
        {
            ui->setupUi(this);
            QPlainTextEdit *editor = ui->plainTextEdit;
            QFile f(file);
            if(f.open(QIODevice::ReadOnly|QIODevice::Text)){
                QTextStream  in(&f);
                editor->setPlainText(in.readAll());
            }
        }
        
        TxtEditor::~TxtEditor()
        {
            delete ui;
        }
        
        

        I use it as

        TxtEditor *editor = new TxtEditor(NULL,"/boot/System.map-4.3.3");
            editor->show();
        

        I want to free all the memory used by editor when I close the editor window.
        How to do that?

        ValentinMicheletV Offline
        ValentinMicheletV Offline
        ValentinMichelet
        wrote on last edited by
        #3

        @Mr-Pang
        Where do you create your TxtEditor? Can't it have a parent widget?
        If you can set your widget a parent, then let Qt mechanisms handle the proper deletion for you.
        Maybe you can consider creating your TxtEditor on the stack, depending on your scope.

        1 Reply Last reply
        0
        • M Mr Pang

          overload

          void TxtEditor::closeEvent(QCloseEvent *)
          {
              deleteLater();
          }
          

          Is this OK?

          M Offline
          M Offline
          Mr Pang
          wrote on last edited by
          #4

          overload closeEvent seems to work.

          1 Reply Last reply
          0
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            There's an attribute specifically for that purpose:

            TxtEditor *editor = new TxtEditor(nullptr, "/boot/System.map-4.3.3"); //you should really put "parent" as last param
            editor->setAttribute(Qt::WA_DeleteOnClose);
            
            1 Reply Last reply
            1

            • Login

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