PDA

View Full Version : Menus with overlays



vegeta1in
27th December 2017, 23:56
Hi all,

I am developing an application in which the content changes according to menu options located on top and bottom of screen(see picture attached).
I want to keep these menu options common through all the screens, but only want to change the content in the middle of these pages. Can you let me know the best possible way to do this?

I tried stack widgets but we have many screens with significant code associated with them, which might cause maintenance issues with a large chunk of code in a single file.

Any other suggestions?

Thanks.

d_stranz
28th December 2017, 17:16
which might cause maintenance issues with a large chunk of code in a single file.

Why do you think that you must put all of the code into a single file if you use a stack widget? A stack widget is simply a container for other widgets that is configured so that only one of those widgets is visible at any time.

The code for each of the widgets managed by the stack widget can (and almost always should) be defined as a separate class, usually derived from QWidget, and implemented into its own set of source, header, and form (.ui) files. If these classes need to communicate with other parts of your app, then they should emit signals when significant things happen, and the code that creates the stack widget and its contents should connect those signals to slots implemented by the other parts of your application that need to know about those changes.

You will almost never need to derive a custom class from QStackedWidget (or QTabWidget). You may have a custom class that owns the stack widget and constructs its contents, but the stack widget doesn't care or need to know anything about the widgets it manages.

vegeta1in
28th December 2017, 21:52
Got it working. Thanks a lot!
Also referred to this video:
https://www.youtube.com/watch?v=27PvtxWlV-o

d_stranz
28th December 2017, 23:35
That is a terrible video, and I would be very cautious about using any of that poster's videos as a guide. Just two of the things I saw that were serious errors:

1 - He defined his stack widget page widgets as automatic member variables of the Application class. (i.e. MachineState machineState; instead of MachineState * machineState). No QWidget instance should ever be declared in this way; instead they should be declared as pointer variables, constructed on the heap (using new()) and given an appropriate parent:



// .h

class MachineState;
class Settings;

class Application : public QWidget
{
Q_OBJECT;

// ...

private:
MachineState * machineState;
Settings * settings;

};

// .cpp

Application::Application( QWidget * parent )
: QWidget( parent )
// ...
{
ui->setupUi( this );
machineState = new MachineState( this );
settings = new Settings( this );

//...
}


with this code, the two sub-widgets are owned by the Application instance and will be appropriately destroyed when Application is destroyed.

2 - He doesn't use layouts for any of the page widgets, and he puts the control widgets on the forms with absolute positions and sizes. This will result in ugly behavior when the application widget is resized.

There are probably more things that would make me ill, but just watching that much made me sick enough.

Maybe you know enough to use better coding practices than the ones in this video, but if you thought that what you saw was just fine to imitate, well, don't do that. Study the Qt tutorials and examples for much better solutions.