Which class should take care of labels?

I am coding a simple text editor with several basic features. Currently it has a QMainWindow with QTabWidget set to centralWidget . Now I am implementing a few shortcuts; ctrl+s to save, ctrl+o to open and ctrl+t/ctrl+w to create a new tab / close the current tab.

I know how to implement all the functions, but the question is: where to implement the functions? Should all shortcuts be members of QMainWindow and let it take care of everything, or should I try to highlight shortcuts in my matching classes? For example, creating a new tab ( ctrl+t ) will be part of QTabWidget . Now what about ctrl+s (save, duh) if it will be part of QTextEdit , as this is the text I save, or ...?

Basically my program contains three classes; QMainWindow , which contains a QTabWidget , and each tab is a QTextEdit .

+4
source share
1 answer

Your setup is very similar to many of the applications I created.

I usually handle shortcuts using the QAction::setShortcut() method, so it really is more about where it makes sense to store QAction objects. Usually this is my MainWindow class, since many actions are used in the MainWindw menu. When these actions are triggered, the result is usually that the TabWidget is notified, which in turn notifies all the necessary tabs and can handle things like the "Close All" action, which triggers only one save prompt.

However, if it makes no sense to store these QAction in the MainWindow object, then I do not do this, as in the case of the context menu, which is usually available on separate tabs.

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1489460/


All Articles