4

The MainWindow class in GUI such as Qt applications is usually very large, including all the menus, toolbars, central widget, and other widgets and lots of member functions corresponding to all kinds of events and lots of other member convenience functions to do this or that.

Is there any good strategy to simplify the class?

Thanks a lot!

Jayden

user1899020
  • 13,167
  • 21
  • 79
  • 154

1 Answers1

4

The same way you would break down any large component:

Abstract (break down) the functionality and responsibilities into subcomponents; repeat until a suitable level of granularity is achieved.

Without any specific details it's hard to be more precise.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • Thanks for the very good point. But it seems difficult to do it on a main window class because its member functions usually use several components. For example, breakdown a main window into component 0 to 5. Put a related function in component 0. But this function will use some other components. How to let component 0 to access the other components? One way is to store the pointers to the other components in component 0. The other way is to store the pointer pointing to the main window in component 0 and make them accessible in main window class. Which one is better? Or any other better ways? – user1899020 Dec 12 '12 at 21:17
  • By passing a reference of the main window into each sub-component, which in turn passes it into it's sub-components, and so on - that way every component has the ability to access any other. Alternatively if you can guarantee there will only be one instance of the main window, you provide access to it from a singleton. – cmannett85 Dec 12 '12 at 21:17
  • It is a good way. But usually the sub-components in the main window is private. Is making them accessible safe? – user1899020 Dec 12 '12 at 21:28
  • You will have to pick and choose which components you feel can be exposed and which cannot. Also it's usually not necessary to expose the object itself, but the behaviour it represents. As a very simple example: You wouldn't expose a `QPushButton` owned by the window, but you would expose the slot it fires when pressed. – cmannett85 Dec 12 '12 at 21:42