PDA

View Full Version : QStackedWidget and QTabWidget Structure Advice



certqt
15th November 2010, 20:21
I am developing an application that is essentially a way of entering data into many SQL tables in a structured format.

The current layout consists of a QStackedWidget driven by a QListView which contains the tables within the database structure. There will be about 15 pages in the stacked widget.

On each QStackedWidget page is a QTabWidget, each tab of which contains a some of the fields from the table, or perhaps links to one of the other tables. Each tab widget has approx. 5 tabs with approx 10 fields per tab.

If I follow this approach, the result a QMainWindow with approximately 15 x 5 x 10 = 750 widgets + container widgets and labels. Whilst 1000+ widgets in a medium sized application is not unreasonable - having them all in one QMainWindow subclass seems a bit crazy!

My thoughts are to promote each tab to a QWidget subclass which provides all the functionality for that tab page. The problem is though that I like Qt Designer, but all the widgets are children of main window's ui.

I would welcome any comments on how best to structure this monster!

ChrisW67
15th November 2010, 21:12
Create a QWidget derived class, using Designer, for each stacked widget page content. Using Designer for the main window (list view and stacked widget) use a promoted QWidget to insert each of the custom widgets into the stacked widget.

marcvanriet
15th November 2010, 22:21
Hi,

For each QStackedWidget page you need, create a dialog instead that contains the tabwidget with approximately 5 tabs.
Create your main dialog or mainwindow, and put a QStackedWidget on it.
Instantiate each of you dialogs, and add them to the QStackedWidget.

This way you can easily create your different screens visually in Designer without creating a class with a giant number of widgets in it.

Code snippets from a project of mine :


frmRealParts = new CRealPartsForm(this); // add form with Real parts
ui->twComponents->addTab( frmRealParts, "Realparts" );
frmDistParts = new CDistPartsForm(this); // add form with Distributer parts
ui->twComponents->addTab( frmDistParts, "Distributer" );


I use a tabwidget (ui->twComponents) to put other dialogs in (CRealPartsForm and CDistPartsForm, which are just dialogs). QStackedWidget has a similar method addWidget() afaik. This code is put in the constructor of my main form, after the ui->setupUI(this);

Best regards,
Marc

certqt
16th November 2010, 10:20
Chris, Marc, thank you for your helpful insight.

Marc, your solution is the perfect one for me, as you suggest addWidget does the job:


frmRealParts = new CRealPartsForm(this); // add form with Real parts
ui->stackedWidget->addWidget( frmRealParts, "Realparts" );
frmDistParts = new CDistPartsForm(this); // add form with Distributor parts
ui->stackedWidget->addWidget( frmDistParts, "Distributor" );

Apart from improved partitioning in the designer and code, this approach also has two other very real advantages for me:

1. I can add tabs and stacked widget pages on the fly as the user requires them;
2. If it turns out the UI is better served for some pages as a modal / modeless dialog, I can just call ->exec() or ->show() instead!

Thanks again!