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. [Solved] Memory leak in my Qt code
Forum Updated to NodeBB v4.3 + New Features

[Solved] Memory leak in my Qt code

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.9k Views 1 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.
  • T Offline
    T Offline
    TimAnderson
    wrote on last edited by
    #1

    I am just starting out with C++ and Qt so bare with me if this is a silly question. I am trying to write a small program with three buttons on the right hand side that are always available to the user. As the user clicks the buttons different options of the left will become available. This code compiles and runs however as I navigate the program it is clear there is a memory leak. I am using Qt 5.3 and the Qt creator on Windows 7 and have verified the memory leak with the task manager. Here is the header file:

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QPushButton>
    #include <QComboBox>
    #include <QTableWidget>
    #include <QLabel>
    #include <QGridLayout>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QPushButton *ButtonA;
    QPushButton *ButtonB;
    QPushButton *ButtonC;
    
    QComboBox *Combo;
    QTableWidget *Table;
    QLabel *Label;
    
    QGridLayout *MainLayout;
    QGridLayout *SideGrid;
    
    void RemoveLayout(QLayout *Lay);
    

    private slots:

    void on_ButtonAclick();
    void on_ButtonBclick();
    void on_ButtonCclick();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    And the main:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    ButtonA = new QPushButton("A");
    ButtonB = new QPushButton("B");
    ButtonC = new QPushButton("C");
    
    connect(ButtonA,SIGNAL(clicked()),this,SLOT(on_ButtonAclick()));
    connect(ButtonB,SIGNAL(clicked()),this,SLOT(on_ButtonBclick()));
    connect(ButtonC,SIGNAL(clicked()),this,SLOT(on_ButtonCclick()));
    
    MainLayout = new QGridLayout;
    SideGrid = new QGridLayout;
    
    SideGrid->addWidget(new QLabel("Welcome"),0,0);
    MainLayout->addLayout(SideGrid,0,0);
    
    MainLayout->addWidget(ButtonA,0,1);
    MainLayout->addWidget(ButtonB,1,1);
    MainLayout->addWidget(ButtonC,2,1);
    
    ui->centralWidget->setLayout(MainLayout);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::RemoveLayout(QLayout *Lay)
    {
    if (Lay != 0)
    {
    QLayoutItem *child;
    while ((child = Lay->takeAt(0)) != 0)
    {
    if(child->widget()!=0)
    child->widget()->hide();
    Lay->removeItem(child);
    delete child;
    }
    delete Lay;
    }
    }

    void MainWindow::on_ButtonAclick()
    {
    RemoveLayout(SideGrid);
    SideGrid = new QGridLayout;

    Label = new QLabel("Window A");
    
    SideGrid->addWidget(Label,0,0);
    MainLayout->addLayout(SideGrid,0,0);
    

    }

    void MainWindow::on_ButtonBclick()
    {
    RemoveLayout(SideGrid);
    SideGrid = new QGridLayout;

    Table = new QTableWidget;
    Table->setColumnCount(1);
    Table->setRowCount(1);
    Table->setItem(0,0,new QTableWidgetItem("Window B"));
    
    SideGrid->addWidget(Table,0,0);
    MainLayout->addLayout(SideGrid,0,0);
    

    }

    void MainWindow::on_ButtonCclick()
    {
    RemoveLayout(SideGrid);
    SideGrid = new QGridLayout;

    Combo = new QComboBox;
    Combo->addItem("Window C");
    
    SideGrid->addWidget(Combo,0,0);
    MainLayout->addLayout(SideGrid,0,0);
    

    }
    @

    Thanks for your time,

    Tim

    ps. I am aware that I am still using the Qt 4 method for connecting. However the tutorials that I have been working through were using that method and unless there is a good reason to switch over this method is making sense to me. I am not planning on needing anything more complicated than the simple connections above.

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

      Hi and welcome to devnet,

      Unless it's something you really want that way, why not just use a QStackedLayout or QStackedWidget and create your A, B and C widgets only once ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TimAnderson
        wrote on last edited by
        #3

        Thanks for the reply. I will check out those options and make an adjustment. Thanks.

        1 Reply Last reply
        0
        • X Offline
          X Offline
          XHS-SH
          wrote on last edited by
          #4
          This post is deleted!
          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