将变量(ID)传递给pyqt中的另一个已编译的UI
我已经被困扰了一个多小时,不知道如何传递变量,特别是最近访问的 sqlite 中的 ID,以用于生成的另一个 UI。我正在使用 eric4(带有 python、pyqt、qtdesigner 和 sqlite)。
基本上,我正在编写的程序会创建成员,并且在生成成员信息时,每个成员都有一个唯一的 ID。当有新成员产生时,分配给新成员的 ID 必须传递到程序的另一部分。但也有必须对会员进行修改的情况,必须知道ID才能显示会员的正确信息。
基本上,当添加新成员时,它首先将信息输入数据库中。我在名为 newmember.py 的 ui 对话框代码中所做的就是这样:
def on_button_Save_released(self):
Nik = unicode(self.LineEdit_Nickname.text())
self.NMem = NewMem()
self.NMem.input_data(Nik)
self.close()
NewMem 是另一个 py 文件中的一个类,可以访问数据库。 input_data 方法的部分内容如下:
cur.execute("insert into Members (Nick) values (?)",(Nik))
我添加了此代码,以便它知道新成员分配的 ID::
CurrentID = cur.lastrowid
return CurrentID
所以我在 ui 中更改了这一行 self.NMem.input_data(Nik)
对话框代码 newmember.py 放入其中,
ID = self.NMem.input_data(Nik)
以便传递 ID。
现在,对话框代码将打开另一个窗口,我希望将返回的 ID 用于另一个 ui。基本上 newmember.py 中 ui 对话框代码中的整个方法是这样的:
def on_button_Save_released(self):
Nik = unicode(self.LineEdit_Nickname.text())
self.NMem = NewMem()
ID = self.NMem.input_data(Nik)
self.close()
self.Pts = Points()
self.Pts.show()
Points()
是另一个 ui 对话框代码中的一个类,它将显示在 Points
中ui 会员信息。但返回的 ID
信息必须传递给 Points()
ui,以便显示正确的信息。我有预感,我需要更改编译的 ui 表单中的某些部分,以便它知道要显示的 ID
但我如何将它传递到编译的 ui 表单中?
非常感谢任何帮助深深鞠躬
编辑:这段代码中有没有办法 -
self.Pts = Points()
self.Pts.show()
变量 ID 也可以合并并传递到 ui 中吗?这样
self.Pts = Points
self.Pts.show(ID)
ID 也会进入 ui 吗?
Edit2:有没有一种方法可以在 python 中的 ui 类中传递变量,就像这里显示的那样 - http://www.daniweb.com/forums/thread64758.html
I've been stumped for more than an hour on how to pass a variable, specifically an ID in sqlite which was recently accessed, to be used to another UI generated. I'm using eric4 (with python, pyqt, qtdesigner and sqlite).
Basically the program I'm coding makes members and each member has a unique ID when the information of the member is generated. When there is a new member made, the ID assigned to the new member must be passed to another part of the program. But there are also instances where there must be modifications made in the member, the ID must be known to display the right information of the member.
Basically, when a new member is added, it first inputs the the information in the database. What I did is like this in the dialog code of that ui named newmember.py:
def on_button_Save_released(self):
Nik = unicode(self.LineEdit_Nickname.text())
self.NMem = NewMem()
self.NMem.input_data(Nik)
self.close()
The NewMem
is a class in another py file which has access to the database. Parts of the input_data method goes like this:
cur.execute("insert into Members (Nick) values (?)",(Nik))
I added this code so that it will know what ID the new member is assigned::
CurrentID = cur.lastrowid
return CurrentID
So I changed this line self.NMem.input_data(Nik)
in the ui dialog code newmember.py into this
ID = self.NMem.input_data(Nik)
so that the ID will be passed.
Now, the dialog code will open another window and I want the returned ID be used to another ui. Basically the whole method in ui dialog code in newmember.py is like this so far:
def on_button_Save_released(self):
Nik = unicode(self.LineEdit_Nickname.text())
self.NMem = NewMem()
ID = self.NMem.input_data(Nik)
self.close()
self.Pts = Points()
self.Pts.show()
The Points()
is a class in another ui dialog code which will show in the Points
ui the information of the member. But the returned ID
information must be passed to the Points()
ui so that the right information be displayed. I have a hunch that I would be needing to change some parts in the compiled ui form so that it knows the ID
to be displayed but how will I pass it in there compiled ui form?
Any help is very much appreciated bows deeply
Edit: Is there a way in this code -
self.Pts = Points()
self.Pts.show()
be the variable ID be also incorporated and will pass into the ui? Like
self.Pts = Points
self.Pts.show(ID)
that the ID will be going to the ui also?
Edit2: Is there a way how to pass variables in classes of a ui in python just like how it was shown here - http://www.daniweb.com/forums/thread64758.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信使用 Qt 模型和 QDataWidgetMapper 是解决这个问题的 Qt 方法。 这里是一个示例。 (如果您想自己对数据库通信进行编程,则为 QSLTableModel 或 QAbstractTableModel 的子类)。
您还可以查看 Qt 文档中的 SQL Widget 映射器示例。
就我个人而言,我更喜欢使用自定义模型,因为我之前曾使用 Qt 的 SQL 偶然发现过部署问题,但您的情况可能会有所不同。
I believe using a Qt model with a QDataWidgetMapper is the Qt way of solving this problem. An example here. (QSLTableModel or subclass QAbstractTableModel if you want to program the database communication yourself).
You can also check SQL Widget mapper example in the Qt docs.
Personally I prefer to use a custom model because I've previously stumbled upon deployment problems using Qt's SQL, but your mileage may vary.