1

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?

jramm
  • 6,415
  • 4
  • 34
  • 73
  • How about putting everything inside a class, and making `chart` a member of this class? – user3419537 Jul 24 '14 at 09:40
  • how does that help? `chart` itself is a class, inherited from matplotlib's FigureCanvas which is a subclass of QWidget I believe. `chart` is currently instantiated in my `Main` class which is an instance of QMainWindow as that is where the `chart` widget 'resides' – jramm Jul 24 '14 at 09:44
  • 1
    The idea being that you have methods within this class that act on the other widgets and provide `chart` when required, rather than making it a member of each and every widget. Maybe I'm misunderstanding your problem, but thoughts of using globals/singletons are often a sign you may need to reconsider the structure of your code. What is the purpose of `chart`? Maybe you could post some code to make your intentions clearer. – user3419537 Jul 24 '14 at 11:14

1 Answers1

2

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.

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104