将字符串转换为对象实例名称 - python
我可能会把这一切都搞错了,但是...
我正在尝试使用 QAbstractItemModel 从 SQL 数据填充 QTreeView (而且在理解它方面遇到了很大的麻烦)。我正在遵循的教程之一(最简单的)通过简单地调用“节点”的新实例并从列表中生成模型来填充树。该节点有一个名称和一个父节点(如下所示)。当您在程序中生成数据时,这是可以的。我可以遵循这一点:)
但是,我想从表中引入数据并使用字符串来标识正确的父节点 - 主要是因为如果我迭代记录,我将无法使用一个单独的变量(?)。它将用于recs中的x:node = Node(“name”,parentnode)。
当我这样做时,我收到明显的错误消息,表明该字符串不是正确的对象并且没有方法。有没有一种方法可以使用从我的表派生的字符串来识别正确的“父”对象(或者可以,或者有人可以向我指出一个非常基本的 Qtreeview 模型教程,该教程是为非常热情但不一定有天赋的学习者设计的) 。
rootNode = Node("Hips")
childNode0 = TransformNode("RightPirateLeg", rootNode)
childNode1 = Node("RightPirateLeg_END", childNode0)
childNode2 = CameraNode("LeftFemur", rootNode)
childNode3 = Node("LeftTibia", childNode2)
childNode4 = Node("LeftFoot", childNode3)
childNode5 = LightNode("LeftFoot_END", childNode4)
我意识到我可能还没来得及走到这里就开始跑步了,为我的无知提前道歉。
I'm probably going about this all wrong but...
I am trying to populate a QTreeView from SQL data - using QAbstractItemModel (and having a great deal of trouble understanding it tbh). One of the tutorials I am following (the simplest) populates the Tree by simply calling new instances of the 'Node' and generating the model from the list. The Node has a name and a parentnode (as below). This is OK where you are generating the data within the program. This I can just about follow :)
However, I want to bring the data in from the table and use a string to identify the correct parentnode - mainly because if I am iterating over the records I won't be able to name each one using a separate variable(?). It will be for x in recs: node = Node("name", parentnode).
When I do this, I get the obvious error message that the string isnt the correct object and has no methods. Is there a way of using a string derived from my table to identify the correct 'parent' object (either that, or could somebody point me in the direction of a very basic Qtreeview model tutorial designed for very enthusiastic, but not necessary gifted learners).
rootNode = Node("Hips")
childNode0 = TransformNode("RightPirateLeg", rootNode)
childNode1 = Node("RightPirateLeg_END", childNode0)
childNode2 = CameraNode("LeftFemur", rootNode)
childNode3 = Node("LeftTibia", childNode2)
childNode4 = Node("LeftFoot", childNode3)
childNode5 = LightNode("LeftFoot_END", childNode4)
I realise that I am probably running before I can walk here and apologise in advance for my ignorance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串是全局变量的名称吗?如果是这样,您可以使用
globals()['name']
访问全局变量引用的值(将'name'
替换为变量的字符串名称)当然。)或者,更好的是,不要在全局命名空间中乱扔变量名称
您可以使用
dict
:这使得在字符串名称和值之间映射变得非常容易。
Are the strings the names of global variables? If so, you can access the value refenced by the global variable with
globals()['name']
, (replacing'name'
with the string name of the variable of course.)Or, better yet, instead of littering variable names all over your global namespace
you could use a
dict
:This makes it very easy to map between string names and values.