PDA

View Full Version : subwindows in application - memory usage



Tomasz
22nd July 2010, 15:51
Hello!

I'm creating simple measurement application which has some windows showing measured data. I'm concern about my device resources. What happening when I'm closing that extra windows? Are they still in memory?

One of my simple window looks like this (the others are alike):



class Pomiary : public QDialog
{
Q_OBJECT

public:
Pomiary(QWidget* parent=NULL);

private:
void setupUI(); //layout of the window
//some varialbles
};


That windows have a lot of variables (each one). On my main window I've got QPushButtons that opens that extra windows like this:



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

OknoPomiary = new Pomiary;

QPushButton *button = new QPushButton;
ui->appLayout->addWidget(button,0,0,Qt::AlignHCenter);

connect(button,SIGNAL(clicked()),OknoPomiary,SLOT( show()));


Is it correct way to save my resources? Or maybe application with extra windows should be done in other way? Maybe creating my widow in some kind of extra slot invoked by clicking on button?

thanks in advance
best regards
Tomasz

agathiyaa
22nd July 2010, 16:27
When you close the window, memory will not be released. But you may use setAttribute(Qt::WA_DeleteOnClose) in your Pomiary constructor to free the memory when you close your window.

Tomasz
22nd July 2010, 22:43
And what if I want to open again that window? Variables will be 'clean'?
Should I create object of Pomiary class in Main Window constructor, on in the other function invoked by for example button or it doesn't make any difference?

thanks in advance
best regards
Tomasz

ChrisW67
23rd July 2010, 00:31
If you delete the object (directly or with Qt::WA_DeleteOnClose) then OknoPomiary becomes invalid and you need to allocate a new instance in order to show it again. The new instance is independent of the old and will have whatever state the constructors give it.

You currently allocate storage for a Pomiary instance regardless of whether the user has pressed the button to show it (whether this is a big thing is up to you). You could place the logic to allocate and show the window into a slot the push button triggers. The slot could check to see if the window already exists and simply show() it if it does and allocate and show if it does not.

wysota
23rd July 2010, 02:16
It's really funny to see an Aussie use Polish words in English written posts ;) "a Pomiary instance" sounds great ;)

ChrisW67
23rd July 2010, 05:21
I aim to please ;) There is an awful lot of language mangling going around in these forums.

Tomasz
23rd July 2010, 09:13
The slot could check to see if the window already exists and simply show() it if it does and allocate and show if it does not.

So It should be simple if(OknoPomiary) { OknoPomiary->show() } and else create new "a Pomiary instance"? And all in one slot triggered by a button.

I know Polish names sounds funny, next time I will use English names ;-)

thanks in advance
best regards
Tomasz