简体   繁体   English

在Qt Widgets中分离应用程序逻辑和视图

[英]Separating application logic and view in Qt Widgets

I was wondering: what is the 'official' coding style when programming a Qt Widgets application. 我想知道:编写Qt Widgets应用程序时的“官方”编码风格是什么。 I'm specifically referring to keeping logic and view code separate. 我特指的是将逻辑和视图代码分开。

The default Qt Designer way makes it very tempting to just throw everything in one class. 默认的Qt Designer方式使得将所有内容都放在一个类中非常诱人。 In fact, there's a lot of code examples out there that show you can consider MainWindow.cpp as a 'throw all your globals in here' class. 实际上,有很多代码示例表明你可以将MainWindow.cpp视为“在这里抛出所有全局变量”类。

As a simple example, your application can require a QNetworkAccessManager . 举个简单的例子,您的应用程序可能需要QNetworkAccessManager You can store this in your MainWindow and then pass it to any window you open. 您可以将其存储在MainWindow ,然后将其传递给您打开的任何窗口。

But this is the start of putting a whole lot of other logic in your MainWindow . 但这是在MainWindow中放入大量其他逻辑的开始。 If you then want to convert that application to QML, you have a problem. 如果您想将该应用程序转换为QML,则会出现问题。 If you had created a controller class that in turn instantiates the Window or widget, or accepts the controller as constructor argument, so that you can connect view and controller with signals and slots, you would have been able to replace the view with something else, yet retain the logic. 如果你创建了一个控制器类,然后实例化Window或widget,或者接受控制器作为构造函数参数,这样你就可以用视图和插槽连接视图和控制器,你就可以用其他东西替换视图了,但保留逻辑。 Seems a lot better to me, yet doesn't seem to be the norm. 对我来说似乎更好,但似乎不是常态。

Is it still considered bad practice to use widget/window classes (derived from QMainWindow , QWidget ) for logic, even though you see it everywhere? 是否仍然认为使用窗口小部件/窗口类(从QMainWindowQWidget派生)用于逻辑是不好的做法,即使你到处都看到它?

I think it's not a good idea to bring all of the code in just one class or one even one module. 我认为将所有代码只放在一个类或一个甚至一个模块中并不是一个好主意。 You would encounter many difficulties changing parts because one part is changed specially when your application becomes larger over time. 更换零件会遇到很多困难,因为当您的应用程序随着时间的推移变大时,会更换一个零件。

I personally use Subdirs to separate parts of my code (Logic, GUI). 我个人使用Subdirs来分离我的代码部分(逻辑,GUI)。 Using Subdirs is a good idea to separate code modules from each other. 使用Subdirs是将代码模块彼此分开的好主意。 This way you can have independent software modules which are reusable and able to be changed easily. 这样,您就可以拥有可重复使用且可以轻松更改的独立软件模块。 It also makes the project much cleaner and easier to read. 它还使项目更清晰,更易于阅读。

In this approach the modules can interact with each other through signals and slots which makes different components completely independent and changing one module does not require changing other parts. 在这种方法中,模块可以通过信号和槽相互作用,这使得不同的部件完全独立,并且改变一个模块不需要改变其他部件。

Qt Creator provides good automation in liking the parts to each other. Qt Creator提供良好的自动化,使部件彼此喜欢。 You can make a Subdirs project and add your subprojects to its .pro file : 您可以创建Subdirs项目并将子项目添加到其.pro文件中:

TEMPLATE = subdirs

CONFIG += ordered

SUBDIRS += \
    Component1 \
    Component2 \
    Component3 \
    MainApp \

You should bring the subprojects that others depend on, first in the list. 您应该首先在列表中引入其他人所依赖的子项目。 Also notice that the name of the .pro file of the subproject should be the same as it's folder name. 另请注意,子项目的.pro文件的名称应与其文件夹名称相同。 This way the subprojects are detected and listed in the Projects pane. 这样可以检测子项目并在“项目”窗格中列出。

The subprojects Component1 , Component2 and Component3 could be libraries. 子项目Component1Component2Component3可以是库。 Part of .pro file for Component1 : Component1的.pro文件的一部分:

TARGET = Component1
TEMPLATE = lib

DEFINES += COMPONENT1_LIBRARY

SOURCES += ...
HEADERS += ...

The subproject MainApp can be app. 子项目MainApp可以是应用程序。 Part of .pro file for MainApp : MainApp的.pro文件的MainApp

TARGET = MainApp
TEMPLATE = app

You can use the libraries in each subproject by linking it to the subproject. 您可以通过将每个子项目链接到子项目来使用它们。 This can be done by right clicking on the subproject and choosing Add Library and then Internal Library . 这可以通过右键单击子项目并选择“ Add Library然后选择“ Internal Library When you select one library from the list of subprojects, the linking configurations are added to the .pro automatically. 从子项目列表中选择一个库时,链接配置会自动添加到.pro中。 It will be like : 它会像:

win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Component1/release/ -lComponent1
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Component1/debug/ -lComponent1
else:unix: LIBS += -L$$OUT_PWD/../Component1/ -lComponent1

INCLUDEPATH += $$PWD/../Component1
DEPENDPATH += $$PWD/../Component1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM