PDA

View Full Version : StackedWidget, single-window UI - how to separate code for readability?



ngoonee
28th November 2014, 08:28
I'm just starting out with QT, and my programming experience is heavily algorithm-based (machine vision research) rather than application/interface work.

I'm looking at designing an interface for an image processing robotic application. The main window is fine, but I want other windows as well (for example, for tweaking image processing parameters and robot communication testing. Based on searching around, I figured a QStackedWidget would handle this fine, and from a UI-design perspective it certainly does.

However, I find that my app.cpp (and to a lesser extent app.h) file is getting pretty large with many functions (affecting readability). I'd like to split it up, and would like advise on the advantages/disadvantages of the methods I'm thinking about.

1. I could maintain the current large app.cpp but preface functions with page1_ or page2_ (and group all functions for a page together) so I'd know where to look for stuff.

2. I could design multiple forms (.ui files) and put one in each page of the stacked widget. Actually, can I? I'm 75% sure I can, but please correct me if I'm wrong on this.

3. I could leave the form as-is but create one class per page (not a QWidget class) which is responsible only for the widgets in that page. This seems identical to 1 except for the physical location of the code, not improving any separability/reusability at all. [1] seems to talk a bit about this.

If there's any options I could use instead, please do suggest them. Main caveat is I'm pretty sure I prefer to design the UI in Designer rather than in code just so I can see what is placed relative to other things.

Thanks for your time.

[1] - http://www.qtcentre.org/threads/50158-How-to-separate-functionality-of-tab-pages-from-Qt-main-window-class

anda_skoa
28th November 2014, 15:48
2. I could design multiple forms (.ui files) and put one in each page of the stacked widget. Actually, can I? I'm 75% sure I can, but please correct me if I'm wrong on this.


This is the usual approach.

The widget that holds the stack widget only has to deal with the stack widget (or any sibling widgets).
The contents of each page are handled by their respective implementation classes.

Cheers,
_

ngoonee
29th November 2014, 05:18
This is the usual approach.

The widget that holds the stack widget only has to deal with the stack widget (or any sibling widgets).
The contents of each page are handled by their respective implementation classes.

Cheers,
_

Thank you, I'll pursue this. So I'd have (for example) a base.ui/cpp/h combination of source files which would hold the qstackedwidget and my main menu, then I'd have to have each page's widgets added (using addWidget) in the constructor? Doesn't seem to me like there's any way to (in Designer) look at how the whole UI looks like (the base.ui stackedwidget and menu and whatever other widgets are there plus the contents of the qstackedwidget), is that correct?

Edit: I can't seem to addWidget, but I'm able to (for my base.ui qStackedWidget) add a new page and promote the QWidget in that page to be my own widget. Is there an error with my widget code? It's just the basic code created by the Qt 'add item' wizard.


#ifndef MAINPAGE_H
#define MAINPAGE_H

#include <QWidget>

namespace Ui {
class mainPage;
}

class mainPage : public QWidget
{
Q_OBJECT

public:
explicit mainPage(QWidget *parent = 0);
~mainPage();

private:
Ui::mainPage *ui;
};

#endif // MAINPAGE_H





#include "mainpage.h"
#include "ui_mainpage.h"

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

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

anda_skoa
29th November 2014, 09:57
addWdget() in the constructor should work.

How does "I can't seem to addWidget" manifest itself?

Cheers,
_

ngoonee
1st December 2014, 15:53
addWdget() in the constructor should work.

How does "I can't seem to addWidget" manifest itself?

Cheers,
_

Sorry for the late reply. I fixed it, and have been moving ahead with my (halfway completed) app.

I was unable to have a private myWidget object in my base class, but fixed that by using a myWidget pointer instead. Then in the constructor I just did the following:-


page1 = new myWidget;
ui->stackedWidget->addWidget(page1);

This worked. Previous I was trying to do stuff like:-


page1 = myWidget(); and that would not be added properly.