英文:
How can i pass data between classes python
问题
在Entrega
类中有信息我想要传递到Menu
类,这个信息是:
self.projeto = self.ui.comboBoxProjeto.currentIndex()
self.idproj = self.ui.comboBoxProjeto.itemData(self.projeto)
self.idproj
是一个ID,可以是 1
, 2
, 3
等等。
我如何将 self.idproj
传递给 Menu
类?
英文:
I have two classes one for the MainWindow
and other for a QDiaalog
:
class Menu(QMainWindow):
logginuser= None
def __init__(self,):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
The class Entrega
that belongs to QDialog
is called through a button that connects to this function:
def switch_windows(self):
self.entrega2 = Entrega(self.tableLinhaNova)
And the class Entrega
:
class Entrega(QDialog):
def __init__(self, source_table_widget,parent=Menu):
QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
But in the class Entrega
there is information that I want to pass to the Menu class, which is this:
self.projeto=self.ui.comboBoxProjeto.currentIndex()
self.idproj = self.ui.comboBoxProjeto.itemData(self.projeto)
self.idproj
is an Id, it can be 1
,2
,3
etc...
How can I pass self.idproj
to Menu
class
答案1
得分: 1
# 你需要为QDialog创建一个自定义信号。
class Entrega(QDialog):
id_changed: pyqtSignal() = pyqtSignal(int) # 在此处添加信号
def __init__(self, source_table_widget, parent=Menu):
QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
# 当你的id发生改变时,你需要发射该信号,并携带你的id:
self.projeto = self.ui.comboBoxProjeto.currentIndex()
self.idproj = self.ui.comboBoxProjeto.itemData(self.projeto)
self.id_changed.emit(self.idproj) # 在此处发射信号。
# 现在,当信号被发射时,你需要连接到它以获取发射的id:
def switch_windows(self):
self.entrega2 = Entrega(self.tableLinhaNova)
self.entrega2.id_changed.connect(lambda id: self.do_something_with_id(id)) # 在此处获取id。
# 我还没有测试过代码。但这可能有助于理解如何获取id的流程。希望对你有所帮助。
英文:
You have to create a custom signal for the QDialog.
class Entrega(QDialog):
id_changed: pyqtSignal() = pyqtSignal(int) # Add this signal
def __init__(self, source_table_widget, parent=Menu):
QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
When your id changed, you have to emit that signal with the id that you have:
self.projeto = self.ui.comboBoxProjeto.currentIndex()
self.idproj = self.ui.comboBoxProjeto.itemData(self.projeto)
self.id_changed.emit(self.idproj) # Emit signal here.
Now, when the signal emitted, you have to connect to it to get the emitted id:
def switch_windows(self):
self.entrega2 = Entrega(self.tableLinhaNova)
self.entrega2.ic_changed.connect(lambda id: self.do_something_with_id(id)) #Get id here.
I have not tested the code yet. But this might helps to have the flow of how you get the id. Hope it helps you.
答案2
得分: 0
有实际上有几种在类之间交换数据的方法。
以下是最简单示例的样板(使用构造函数):
class A:
def __init__(self, data):
self.data = data
class B:
def __init__(self, a):
self.a = a
a = A("Hello")
b = B(a)
print(b.a.data) # 输出:Hello
您也可以通过以下方式实现:
- 使用方法
- 使用属性
英文:
There are actually several ways of exchanging data between classes.
Here is a boiler plate of the simplest example (using the constructor):
class A:
def __init__(self, data):
self.data = data
class B:
def __init__(self, a):
self.a = a
a = A("Hello")
b = B(a)
print(b.a.data) # Output: Hello
You can also do this by:
- using a method
- using a property
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论