2

I have a graphical application written in C++ using Qt for an embedded device, which uses a QStackedWidget holding a number of UI widgets. The UI is all designed in Qt Creator's designer tool. When the user navigates through the device's application the display to be shown at that menu option is selected from the QStackedWidget and this all works great.

I'm now wanting to pass in a pointer to some configuration which is read from file when the application starts, but I can't seem to find a way to pass this pointer as an argument into the constructor of a widget on the QStackedWidget. Can anyone help?

My current approach is to call a function I've written within the class of a widget on the QStackedWidget, which works but doesn't feel the best way to do it.

demonplus
  • 5,613
  • 12
  • 49
  • 68
SionHughes
  • 128
  • 11
  • Could you provide a SSCCE? – Dmitry Sazonov Oct 28 '15 at 08:48
  • You will have to set the widget programatically for the QStackedWidget or just implement a method for setting the pointer during stacked-widget creation (MainWindow constructor): ``ui->stackedWidget->widget(index)->setPointerMethod(ptr)`` – Sebastian Lange Oct 28 '15 at 09:10
  • Thanks Sebastian, that's similar to how I'm doing it: `ui->pg_generalSettings->SetConfiguration(configuration);` – SionHughes Oct 28 '15 at 09:20
  • @SionHughes if you intend to only have exactly ONE configuration object (accesible from all over your application) you also could consider using a singleton: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – Sebastian Lange Oct 28 '15 at 09:33
  • That's a far better idea Sebastian. Just implemented and works a treat! Many Thanks. – SionHughes Oct 28 '15 at 09:56

1 Answers1

2

To my knowledge if you want to use custom constructors - with other kinds of arguments than just the QWidget * parent - you have to create the ui programmatically:

  • create your custom StackedWidget with a special constructor,
  • prepare the global interface using the designer,
  • then add the StackedWidget in the constructor of the class after the setupUi method.

The other way is to use an initialization method after the construction of the item, like you did.

Davy
  • 429
  • 2
  • 17