[Solved] Memory leak in my Qt code
-
wrote on 6 Aug 2014, 19:00 last edited by
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_OBJECTpublic:
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.
-
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 ?
-
wrote on 6 Aug 2014, 22:24 last edited by
Thanks for the reply. I will check out those options and make an adjustment. Thanks.
1/3