PDA

View Full Version : Doubt about QLabel * usage.



chobin
17th July 2013, 15:10
Hi everyone,

I am developing a program which creates dynamically labels and saves their handles in a QList.

The labels are created OnMouse click and are destroyed OnMouse double click.

Actually the program runs without problems, but I wonder if I am doing a correct usage of pointers in order to prevent runtime errors: I would like to know if the memory correspondent to the created label pointers will exists till mouseDoubleClick_slot is called.

Here below an extract of my code.



QList<QLabel*> *tipList; // declared in the .h file

void CommandWindow::mouseRelease_slot(QMouseEvent* e)
{
QLabel *tip = new QLabel(ui->customPlot);
tipList->append(tip);

// ...
}

void CommandWindow::mouseDoubleClick_slot(QMouseEvent* e)
{
if (tipList->size())
{
for(int i=0; i<(tipList->size()); i++)
{
QLabel *tipTmp = tipList->at(i);
delete tipTmp;
}
tipList->clear();
}
}

wysota
17th July 2013, 15:13
Yes, it will exist.

A simpler alternative to your mouse double click event is:


{
qDeleteAll(tipList);
tipList->clear();
}

By the way, keeping the list itself as a pointer usually does not make sense.

chobin
19th July 2013, 11:41
Hi Wysota,

Thanks for your answer. I have followed you suggestion, I have replaced the * QList declaration with a QList.

My program seems to run as expected. :)