PDA

View Full Version : children of a QFrame: possible to delete?



vonCZ
8th September 2008, 08:20
I have a function:

mkPan();

that makes all the Information panels for my Qt Application. The panels are QFrames with one or more QLabels made up of QStrings and/or QPixmaps. I define the components of a particular panel in my .h file like so:



QFrame *panFra1;
QLabel *panLab1;
QString *panStr1;


I'd like to free-up / clean-up memory by deleting everything but the QFrame after it's been made. I've noticed that I can do this with QStrings: after defining a QLabel and setTexting it with a QString, I can then delete the QString. Presumably the QString is rasterized on the QLabel, and there's thus no longer any need to keep it (the QString) around. (And I can therefore simply create QString's on the stack within the mkPan() function and don't need to declare pointers to them in the .h file.) If I try the same with the QLabels, however: they don't show up on the QFrame. Is there a way to 'rasterize' the QLabels onto the QFrame so I don't need to keep them around? ...or is this even something I should worry about.

One last thing: I'd want to stop declaring the QLabel's in the .h file and simply declare them in the mkPan() function as needed. Is this OK:



mkPan()
{
if(panType == 7)
{
for(int a=0; a<numberOfLabelsThisPanel; a++)
{
QLabel *tempLab = new QLabel;
tempLab->setText(panStr);
// etc...
// delete tempLab; // this I cannot do
}
}
}


...i've left some stuff out here, including methods for getting the appropriate QString value, but the crux of my question is: can I declare "QLabel *tempLab" over and over again... or do they have to be unique names?

aamer4yu
8th September 2008, 09:05
QLabel will be needed as long as u need them. They are widgets.
Qstring is just a class, and to set text on qlabel, u guessed right, u dont have to store a Qstring for it. u just need to call setText.

and how many QLabels are u creating ? u need not worry much about them as Qt is quite efficient, u just need to set the right parent.

Also u can have same name for different labels.

vonCZ
8th September 2008, 09:47
and how many QLabels are u creating ? u need not worry much about them as Qt is quite efficient, u just need to set the right parent.

Thanks for clearing that up, '4yu. The number of QLabels varies from project-to-project: 400-500 not uncommon. For example: the attached image--a screencap from my App--has a pop-up Info panel (QFrame) comprised of 8 QLabels: 7 for the text, and one for the image. There may be 20 such panels--plus other kinds, likewise comprised of many QLabels--scattered throughout a project.

Anyway, by your account I guess it's OK to have 500 pointers to QLabels all with the same name. I'll give it a go. Thanks again.

aamer4yu
8th September 2008, 18:18
If you want to optimize, you can identiify frames / widgets which will not be shown all the time.
You can the create those frames on the go and delete them as soon as the work is over.
For example ur pop up window, i guess its shown on some event. You can create a separate class for the pop up window and create it when some event occur. That way u will save memory. Extra cost is, u will have to make conenctions each time.

Also i just gave a example, am not sure how it fits into ur app needs