简体   繁体   English

Qt避免循环依赖

[英]Qt avoiding circular dependency

I have a Qt application with a main class GUIApplication which inherits from QMainWindow as shown below. 我有一个Qt应用程序,其主类GUIApplication从QMainWindow继承,如下所示。 The GUI has a start button, stop button and a QGraphicsScene. GUI具有一个开始按钮,一个停止按钮和一个QGraphicsScene。

My application will read data from the serial port and other stuff and draw on the QGraphicsScene according to what is received. 我的应用程序将从串行端口和其他内容读取数据,并根据收到的内容在QGraphicsScene上进行绘制。

My main GUI class knows about my serial port class as it must initialse it on press of the start button etc. That is fine. 我的主GUI类知道我的串行端口类,因为它必须在按下开始按钮等时对其进行初始化。这很好。 My serial port class however must also must be able to communicate with my GUI class as it must update the QGraphicsScene on reception of data and other stuff. 但是,我的串行端口类还必须能够与我的GUI类通信,因为它必须在接收数据和其他内容时更新QGraphicsScene。

My question is , is it a bad design for my serial port class to know about the GUI class also. 我的问题是,知道我的GUI类对我的串行端口类来说也是一个不好的设计。 Isn't this a form of circular dependency between the two classes? 这不是两个类之间循环依赖的一种形式吗? I plan to have a member of my serial port class a pointer to the GUI class. 我计划让我的串行端口类的成员一个指向GUI类的指针。 Is this the only way to do it , or should I register a callback in the serial port class instead, to decouple the classes? 这是唯一的方法,还是应该在串行端口类中注册回调以解耦类? Any help is appreciated thanks. 任何帮助表示赞赏,谢谢。

// GUI class
#include "MySerial.h "
class GUIApplication : public QMainWindow
{
    Q_OBJECT

public:
//.........

o_o o_o

// Serial port class
#include "GUIApp.h "
class MySerial
{

public:
//..............
//write something on GUI`

It is a bad design. 这是一个糟糕的设计。 Your serial port class has nothing to do with GUI by nature and as such should be separated from it - apart from circular dependencies, you make it much less flexible that way and limit its usability. 您的串行端口类与GUI本质上无关,因此应将其与GUI分开-除了循环依赖关系之外,您还应使其灵活性大大降低并限制其可用性。 One day you might want to use it in a console application and if you wrote the class in such a way that it must be aware of the GUI class, you would have to modify it just to make it work with console and either have two versions that would differ only regarding UI, which doesn't make much sense, or add Qt as dependency even though you would not need it in console app if you wouldn't want to break the compatibility, which doesn't make much sense either. 有一天,您可能想在控制台应用程序中使用它,并且如果您以必须知道GUI类的方式编写该类,则必须对其进行修改,以使其能够与控制台一起使用,并且有两个版本那只会在没有太大意义的UI上有所不同,或者即使您不想破坏兼容性,也可以在控制台应用程序中不需要Qt作为依赖添加Qt,这也没有多大意义。

Much better solution is sending some signal from your serial port class or some meaningful return code after the data is received, and then make the GUI class catch this value and update QGraphicsScene itself. 更好的解决方案是在收到数据后从您的串行端口类发送一些信号或一些有意义的返回码,然后使GUI类捕获此值并更新QGraphicsScene本身。 That way you also get rid off the circular dependency. 这样,您还摆脱了循环依赖。

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

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