PDA

View Full Version : QMainWindow/centralWidget question



graciano
14th April 2010, 15:56
Hi,

I created 2 small examples with a QMainWindow that allows me to set the centralWidget using 4 widgets i created: graus000, graus090, graus180 and graus270.

I have 2 versions: v1 and v2.

v1 looks like this:


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

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

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

void MainWindow::on_actionVer_a_0_graus_triggered()
{
frm000 = new graus000Form;
this->setCentralWidget(frm000);
}

void MainWindow::on_actionVer_a_90_graus_triggered()
{
frm090 = new graus090Form;
this->setCentralWidget(frm090);
}

void MainWindow::on_actionVer_a_180_graus_triggered()
{
frm180 = new graus180Form;
this->setCentralWidget(frm180);
}

void MainWindow::on_actionVer_a_270_graus_triggered()
{
frm270 = new graus270Form;
this->setCentralWidget(frm270);
}


This works but it seems i'm wasting memory here!?

v2 looks like this:


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

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
frm000 = new graus000Form;
frm090 = new graus090Form;
frm180 = new graus180Form;
frm270 = new graus270Form;
}

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

void MainWindow::on_actionVer_a_0_graus_triggered()
{
this->setCentralWidget(frm000);
}

void MainWindow::on_actionVer_a_90_graus_triggered()
{
this->setCentralWidget(frm090);
}

void MainWindow::on_actionVer_a_180_graus_triggered()
{
this->setCentralWidget(frm180);
}

void MainWindow::on_actionVer_a_270_graus_triggered()
{
this->setCentralWidget(frm270);
}

I set the centralWidget to graus000, then to othe value and then again to graus000 ... and it crashes.

Full examples here ( http://www.box.net/shared/ycvua1jarz ).

What's the best approach for doing what i'm trying to do here?

Thanks

faldzip
14th April 2010, 18:06
Use QStackedWidget as you central widget and add your widgets to this stacked widget.

norobro
14th April 2010, 19:26
From the QMainWindow docs:
Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.In v2 the destructor of the current form gets called when you select a different form, leaving you with a dangling pointer. Thus the segfault. So in v1 you are not wasting memory because the previous form is deleted.

Count my vote for QStackedWidget.

graciano
14th April 2010, 21:32
You mean that ...


frm000 = new graus000Form;

... allocated the memory and the ...


this->setCentralWidget(frm000);

... tells the QMainWindow to takes ownership of the frm000 pointer.

So in this case the "appropriate time" happens everytime i set a new central widget and there is no need for any delete frm000.(?)

Beeing this true the main advantage in using a QStackedWidget will be the speed. And the main disadvantage will be that i will have all widgets loaded in memory while the programs is running ... right?

norobro
15th April 2010, 03:07
So in this case the "appropriate time" happens everytime i set a new central widget and there is no need for any delete frm000.(?)yes, no need.

Beeing this true the main advantage in using a QStackedWidget will be the speed. And the main disadvantage will be that i will have all widgets loaded in memory while the programs is running ... right? Another advantage is the simplification of your code. I didn't know the answer to your memory usage question so I set up a stacked widget version of your app. Surprisingly (to me), the QStackedWidget version executable is slightly smaller and uses less memory.

Executable size:
Your v1 = 1,372,139 B
Stacked Widget = 1,360,267 B

pmap results:
Your v1 = 26,368K
Stacked widget = 26,300K

I am attaching my .cpp .h .pro & .ui files. Give them a test drive and see if I left something out. - 4508

graciano
15th April 2010, 21:52
hummm ... interesting thing this QStackedWidget.
But ... let me "refrase" ...

If i want to build an application based on a QMainWindow, and i don't want to have my dialogs floating on the user Desktop ... what would be the best approach?
The idea is to build independent Dialogs for each task ... and then use them in the appropriate moment by showing them in the "centralWidget".

For now we have:
1. my v1 example
2. QStackedWidget
3. ?

I think that the MDI approch does not fit in this discussion!?

faldzip
15th April 2010, 22:49
[QUOTE=graciano;140033
I think that the MDI approch does not fit in this discussion!?[/QUOTE]
It depends how many widgets you can see at a time. MDI allows you to open many windows in MDI area.
You can use also use QTabWidget. And I didn't know how complicated you 4 widgets are but I think that in your case QStackedWidget is the best solution and memory usage is not such big deal in this case unless you run your app on some PC with 20MB RAM...