Qt best practice: append signals and slots to my main window or create new class

老子叫甜甜 提交于 2019-12-11 04:49:37

问题


Im just pondering best practice with an application I'm developing. Its a simple one window application using qt creator. It's just going to start a QProcess and show the output in a QTextEdit box. To do this there needs to be a bit of processing between the output of the QProcess and the QTextEdit but i dont know where i should do this, should i create a new class to do that or add member functions and extra signals and slots to my main window? I dont want mainwindow to become bloated and hard to read but equally i dont want to have more source files than really I need.

Any thoughts?


回答1:


The main window class can very easily get bloated with all kinds of functionality. I've dealt with that myself, so it's a very real trouble.

Really, though, this is not so much of QT question as it is an object-oriented design question. The key is that your output window does not need to be a part of QMainWindow, so it probably shouldn't be. Make the display a widget, and insert it onto the main window. That is much more flexible, as if you ever need to move that output pane for any reason, it won't be coupled to a specific part of the program.

The logic that feeds data into that output pane should also get its own class, separating the responsibility for displaying the output from the responsibility for acquiring the output.

For reference on the concepts behind my suggestion, see the Single Responsibility Principle and the separation of concerns.




回答2:


Also - you may wish to have a read at this link to model view delegate information which is how I tend to develop in Qt. Like the person above says - this is more a question of good OO design.



来源:https://stackoverflow.com/questions/6239664/qt-best-practice-append-signals-and-slots-to-my-main-window-or-create-new-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!