jramm
jramm

Reputation: 6655

Pyside/PyQt: 'Global' widget?

I am making a simple plotting application in PySide.

The main window has a chart widget which contains my plot figure.

There are various other widgets and dialogs which need to access the chart widget to alter properties of the figure (appearance, plot data etc...).

At the moment, my widgets require the instance of chart to be passed into them when initialised...this is a terrible way of allowing everything to talk to chart as there are chains of this reference being passed around.

Is there a way to make the chart widget 'global' /accessible from any widget?

I have thought about creating a singleton of chart outside of the main window - any reasons why this would be a bad idea?

Upvotes: 2

Views: 1110

Answers (1)

NoDataDumpNoContribution
NoDataDumpNoContribution

Reputation: 10864

One alternative is communication by signals and slots, so the main window has some slots (draw this, draw that, ...) and each of the other widgets has a signal (I want this to be drawn) and then you connect the signals and the slots. This is probably the most decoupled solution and I would prefer it.

You can use a global. No problem with it. Global variables are bad by itself but if this doesn't bother you, go with it.

Finally the idea of user3419537 seems like a good thing to do. You can bundle all the widgets that belong together in one class and then access the chart widget as a class member. This is probably better than a global.

Upvotes: 2

Related Questions