同步 Qt TreeWidgets
我是 Python 和 PyQt 的新手。保持 4 个 QtTreeWidgets 同步以便项目以及所有项目的所有属性相同的最佳方法是什么?这些小部件在会话期间的不同时间出现在不同的对话框中。由于多种原因,我需要尽可能保持现有代码、信号和布局的完整性。模型/视图部门显然是第一个去的地方,但我不想触及任何用于访问或更新树的方法。我计划在几个月内重构整个事情,但在那之前我需要一些快速的东西来支撑我。
由于每个 QTreeWidget 都是一个便利类,因此每个都有自己的数据。 UI 是在 Qt Designer 中维护的,我不想保持这种状态。
当每个对话框初始化时,就会出现树。该应用程序有一个单例类,所有对话框都可以使用它来引用其变量/属性。
在每个父对话框的初始化中,我无法检查单例中是否存在“locationTree”属性。如果没有,我需要用其初始状态填充它,并让对话框中的树使用它或它的副本。每当对话树的状态以我可以捕获的方式更改时,我都想更新单例“locationTree”以反映更改。虽然QTreeWidgetItem上有一个clone方法,但我没有看到整个QTreeWidget的相应方法。
如何通过对现有代码库和 GUI 布局进行最少的更改来实现这一目标?
约翰
I'm new to Python and PyQt. What is the best way to keep 4 QtTreeWidgets synchronized so that the items are the same as well as all the attributes of all the items? These widgets appear in different dialog boxes at different times during a session. For a number of reasons, I need to keep as much of the existing code, signals and layout as intact as possible. The Model/View division would be the obvious first place to go, but I don't want to touch any of the methods that are used to access or update the tree. I'm planning to refactor the whole thing in a few months, but I need something quickly to carry me until then.
Since each QTreeWidget is a convenience class, each has its own data. The UI is maintained in Qt Designer and I don't want to keep it that way.
When each dialog is initialized, the tree appears. The application has a singleton class that all dialogs can use to reference its variables/attributes.
In the initialization of each parent dialog, couldn't I check to see if a 'locationTree' attribute exists in the singleton. If not, I would need to populate it with its initial state and have the tree in the dialog use it or a copy of it. Any time the state of the dialog tree is altered in ways that I can trap, I'd like to update the singleton 'locationTree' to mirror the change. Although there's a clone method on a QTreeWidgetItem, I didn't see a corresponding method for the entire QTreeWidget.
How can I accomplish this with the least amount of change to the existing code base and GUI layout?
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,使用 MVC 设施是正确的方法...
即使您使用 QTreeWidget,您仍然使用从 QAbstractItemView 派生的类,因此
model( )
和setModel()
调用可用。从您正在创建的小部件之一获取模型,然后将其设置在其他小部件中。每当您更改其中一个小部件中的数据时,其他小部件都会效仿,因为它们使用相同的模型实例。如果您需要在所有小部件中保持相同的选择状态(树的哪些部分打开或关闭),这可能会有点困难,但实际上可以通过使用相同的选择模型
selectionModel()
和setSelectionModel()
Yes using the MVC facilities is the way to go ...
Even though you are using
QTreeWidget
you are still working with a class derived fromQAbstractItemView
therefore themodel()
andsetModel()
calls are available. Take a model from one of the widgets that you are creating and then set it in the other widgets. Whenever you change the data in one of the widgets the other widgets will follow suit as they are using the same instance of model.If you need to maintain the same selection state in all for widgets (which parts of the tree are open or close) that might be a little bit harder but it might actually work by using the same selectionModel
selectionModel()
andsetSelectionModel()
我确信您是对的,使用模型/视图是最好的方法。
但是,如果不知道您的树小部件将拥有多少项目以及它们的更新频率,就很难权衡替代方法。另外,你用的是什么版本的Qt?
如果更新和项目的数量不是很大,一种方法是引入一个继承 QObject 的类(因此它具有信号和槽),并使其负责保持所有 QTreeWidget 同步。
通过将每个 QTreeWidget 的信号和槽连接到单个其他对象,您可以避免让每个树小部件都了解其他每个树小部件的噩梦。
I'm sure you're right that using Model/View is the best approach.
But without an idea of roughly how many items your tree widgets will have, and how frequently they'll be updated, it's hard to weigh up alternative approaches. Also, what version of Qt are you using?
If the number of updates and items are not huge, one approach is to introduce a class that inherits
QObject
(so it has signals and slots), and make it responsible for keeping all your QTreeWidgets in sync.By connecting signals and slots for each QTreeWidget to a single other object, you avoid the nightmare of having every tree widget know about every other one.