使用 Qt,有什么好方法可以分解包含 GUI 逻辑的大型源文件? [英] With Qt, what are good ways to break up a large source file containing GUI logic?

查看:47
本文介绍了使用 Qt,有什么好方法可以分解包含 GUI 逻辑的大型源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt 5 使用 C++ 开发一个项目,它有一个包含大量 UI 元素的复杂窗口.此窗口是使用 Qt Designer 设计的,并从 UI 文件加载.

我发现 Qt 的典型模式是在其主要的包含类中编写窗口 UI 的所有逻辑;即他们文档中的单一继承方法".例如,您有一个名为 QMyWindow 的类和一个定义布局的相应 qmywindow.ui 文件,其中 QMyWindow 包含 UI 的所有逻辑,主要在利用自动连接的槽方法内部(即命名为 on_objectName_signalName 之类的方法).对于没有太多复杂性的简单窗口,这很好.然而,对于大型、复杂的窗口,将所有内容放在一个类中的模式开始崩溃,因为文件变得太大且杂乱无章.

我的大而复杂的窗口的源文件现在已经超过 3000 行了.我已经将窗口的所有实际功能分解到其他类中,所以这 3000 行代码主要只是做 UI 的繁重工作.从本质上讲,所有的东西都可以放在窗口类的权限之下.太多了.

使用其他语言和 GUI 工具包,我可以非常有效地将窗口分解为逻辑部分.例如,在 iOS 或 OS X 应用程序中,我可以在故事板文件中使用嵌入式控制器,允许我以逻辑方式分解多个控制器之间的逻辑,并且仍然可以对 UI 本身进行很好的 WYSIWYG 编辑.

但我不知道使用 Qt 做到这一点的好方法.我在 Qt Designer 中没有发现任何东西可以让您将单个 UI 文件分解为由多个类处理的多个部分.如果我自己编写用于创建和布置 UI 的代码,我当然可以这样做,但是 qmake 从设计器文件生成的结果代码会创建一个 ui 命名空间和一个 setupUI 函数,该函数在包含的窗口中创建所有内容.我不知道您如何分解并继续使用自动连接.

Qt 中有一些机制可以动态加载多个设计器文件,然后将它们嵌入到一个小部件中,但是如果我要使用它,我将 a) 显着增加我的代码的复杂性,并且 b) 失去了很好的 WYSIWYG编辑,我不想这样做.

这里有什么好的选择吗?我不想保留一个难以导航的 3000 多行文件,但我不知道如何分解它并仍然使用 Qt 的工具,尤其是 Qt Designer,它们的预期方式.

解决方案

3000 行代码不是问题,不完全确定您为什么不这么认为.

无论如何,在单个单元"中制作非常复杂的 UI 是一个坏主意.您应该将一个巨大的 UI 模块化为不同的 UI,并将它们组合在一起.

总而言之,我建议不要使用 UI 设计器,而是完全用代码编写 UI.但即使您开始使用设计器,也可以注册您的自定义小部件以使用它.

I'm working on a project in C++ utilizing Qt 5, and it has a complex window with large number of UI elements. This window was designed using Qt Designer and is loaded from a UI file.

The pattern that I've found typical of Qt is to write all of the logic for a window's UI in its main, encompassing class; i.e. the "Single Inheritance Approach" from their documentation. For example, you have a class named QMyWindow and a corresponding qmywindow.ui file defining the layout, with QMyWindow containing all the logic for the UI, mainly inside of slot methods that utilize auto-connect (i.e. methods named something like on_objectName_signalName). For simple windows that don't have a lot of complexity, this is fine. However, for large, complex windows, the pattern of putting everything in one class begins to break down because the files get too big and disorganized.

The source file for my large, complex window is over 3000 lines long now. I've already got all of the actual functionality of the window broken into other classes, so these 3000 lines of code are mainly just doing the grunt work of the UI. Essentially, it's all stuff that makes sense to put under the purview of the window's class. There's just too damn much of it.

With other languages and GUI toolkits, I could pretty effectively break the window up into logical pieces. For example, in an iOS or OS X application, I could use an embedded controller in a storyboard file, allowing me to break up the logic amongst multiple controllers in a logical way and still have the nice WYSIWYG editing of the UI itself.

But I'm not aware of a good way to do that using Qt. There isn't anything I've found in Qt Designer that allows you to break up a single UI file into multiple pieces handled by multiple classes. If I were writing the code for creating and laying out the UI myself I could certainly do that, but the resultant code generated by qmake from a designer file creates one ui namespace and one setupUI function that creates everything within the encompassing window. I don't see how you can break things apart and continue to use auto-connect.

There are mechanisms in Qt to dynamically load several designer files and then embed them in a widget, but if I were to use that I would be a) dramatically adding to the complexity of my code, and b) losing the nice WYSIWYG editing, which I don't want to do.

Are there any good options here? I don't want to keep around a 3000+ line file that's difficult to navigate, but I don't see how I can break it up and still use Qt's tools, particularly Qt Designer, the way they were intended.

解决方案

3000 lines of code is not a problem, not entirely sure why you think otherwise.

At any rate, doing very complex UIs in a single "unit" is a bad idea. You should modularize that one huge UI into different UIs and compose them together.

All in all, I would advice against using the UI designer, and instead write the UI entirely in code. But even if you are set on using the designer, it is possible to register your custom widgets for use with it.

这篇关于使用 Qt,有什么好方法可以分解包含 GUI 逻辑的大型源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆