类 已连接但未正确响应
Classes connected but don´t respond properly
我正在尝试连接两种不同 classes 中的两种方法。在名为 "Ventana" 的 class 中,这是我的主要 window,我有这个方法:
def addChannel(self):
global channelCount
self.scrollLayout = QFormLayout()
self.canal = QwtPlot()
self.canal.setLayout(self.scrollLayout)
if channelCount <= 4:
self.splitter1.addWidget(self.canal)
channelCount += 1
print str(channelCount)
在另一个class中,我有这些方法:
class QwtPlotList(QDialog):
def __init__(self):
QDialog.__init__(self)
uic.loadUi("PropiedadesCanal.ui", self)
self.botonAdd.clicked.connect(self.addChannel_2)
def addChannel_2(self):
global channelCount
self.botonAdd.clicked.connect(Ventana().addChannel)
if channelCount <= 4:
self.listWidget.addItem("Canal : " + str(channelCount))
我想做的是,当我按下按钮“botonAdd
”时,“addChannel_2
”方法会调用 "addChannel" 方法,该方法位于Ventana
class。然后,创建了一个QWidget
("self.canal")。
这段代码发生了什么,当我按下“botonAdd
”时,它会在 listWidget
中放置一个项目,但不会创建 QWidget
。如果我在工具栏中创建带有按钮的“QWidget
”,它不会在 QListWidget
中添加任何项目
您可以在这些图片中看到所有这些:
The "botonAdd" creates an item in the QListWidget but not a QWidget
Another button creates the Qwidget, but not the item in the QListWidget
希望你能帮助我。感谢您的时间和回答
有几个问题
1. 从另一个 class 调用 class Ventana
中的函数。你做什么:
self.botonAdd.clicked.connect(Ventana().addChannel)
Ventana()
创建 class Ventana
的新实例。这
按钮 botonAdd
没有连接到您的主按钮 window,它是
连接到您刚刚创建的另一个主 window。这个新 window
未显示,并被销毁 添加 addChannel_2
的末尾
垃圾收集器(因为你没有保留对它的引用)。
要调用右侧 addChannel
,您需要引用您的实际主 window。在 Qt 中,main window 通常是其他小部件的父级。您需要更改 QwtPlotList
的定义,以便它可以有一个父级:
class QwtPlotList(QDialog):
def __init__(self,parent):
QDialog.__init__(self,parent)
#in main window Ventana
self.myDialog=QwtPlotList(self)
然后可以在QwtPlotList
中调用Ventana
的任意方法:
#in QwtPlotList
self.parent().any_method()
2. 将按钮点击连接到多个功能。你做什么:
self.botonAdd.clicked.connect(self.function1)
def function1(self):
self.botonAdd.clicked.connect(function2)
每次点击按钮,都会调用function1
,并将按钮点击连接到另一个函数。这些连接是附加的。第一次单击该按钮时,将调用 function1
并将该按钮连接到 function2
。第二次调用 function1
和 function2
,按钮连接到 function2
。第三次调用 function1
、function2
并再次调用 function2
,等等
您只需调用 function2
,无需连接按钮:
def function1:
function2()
3. 使用全局变量:你应该避免它,通常有更好的解决方案。
我了解到您有两个函数需要使用相同的变量 channelCount
。为了避免全局,我会在 QwtPlotList
中保留对 channelCount
的引用,并将其作为参数传递给 addChannel
def addChannel(channelCount):
channelCount+=1
return channelCount
#QWtPlotList
#init
self.channelCount=3
def addChannel_2(self):
self.channelCount=addChannel(self.channelCount)
我让你想办法把所有东西放在一起:)
我正在尝试连接两种不同 classes 中的两种方法。在名为 "Ventana" 的 class 中,这是我的主要 window,我有这个方法:
def addChannel(self):
global channelCount
self.scrollLayout = QFormLayout()
self.canal = QwtPlot()
self.canal.setLayout(self.scrollLayout)
if channelCount <= 4:
self.splitter1.addWidget(self.canal)
channelCount += 1
print str(channelCount)
在另一个class中,我有这些方法:
class QwtPlotList(QDialog):
def __init__(self):
QDialog.__init__(self)
uic.loadUi("PropiedadesCanal.ui", self)
self.botonAdd.clicked.connect(self.addChannel_2)
def addChannel_2(self):
global channelCount
self.botonAdd.clicked.connect(Ventana().addChannel)
if channelCount <= 4:
self.listWidget.addItem("Canal : " + str(channelCount))
我想做的是,当我按下按钮“botonAdd
”时,“addChannel_2
”方法会调用 "addChannel" 方法,该方法位于Ventana
class。然后,创建了一个QWidget
("self.canal")。
这段代码发生了什么,当我按下“botonAdd
”时,它会在 listWidget
中放置一个项目,但不会创建 QWidget
。如果我在工具栏中创建带有按钮的“QWidget
”,它不会在 QListWidget
您可以在这些图片中看到所有这些:
The "botonAdd" creates an item in the QListWidget but not a QWidget
Another button creates the Qwidget, but not the item in the QListWidget
希望你能帮助我。感谢您的时间和回答
有几个问题
1. 从另一个 class 调用 class Ventana
中的函数。你做什么:
self.botonAdd.clicked.connect(Ventana().addChannel)
Ventana()
创建 class Ventana
的新实例。这
按钮 botonAdd
没有连接到您的主按钮 window,它是
连接到您刚刚创建的另一个主 window。这个新 window
未显示,并被销毁 添加 addChannel_2
的末尾
垃圾收集器(因为你没有保留对它的引用)。
要调用右侧 addChannel
,您需要引用您的实际主 window。在 Qt 中,main window 通常是其他小部件的父级。您需要更改 QwtPlotList
的定义,以便它可以有一个父级:
class QwtPlotList(QDialog):
def __init__(self,parent):
QDialog.__init__(self,parent)
#in main window Ventana
self.myDialog=QwtPlotList(self)
然后可以在QwtPlotList
中调用Ventana
的任意方法:
#in QwtPlotList
self.parent().any_method()
2. 将按钮点击连接到多个功能。你做什么:
self.botonAdd.clicked.connect(self.function1)
def function1(self):
self.botonAdd.clicked.connect(function2)
每次点击按钮,都会调用function1
,并将按钮点击连接到另一个函数。这些连接是附加的。第一次单击该按钮时,将调用 function1
并将该按钮连接到 function2
。第二次调用 function1
和 function2
,按钮连接到 function2
。第三次调用 function1
、function2
并再次调用 function2
,等等
您只需调用 function2
,无需连接按钮:
def function1:
function2()
3. 使用全局变量:你应该避免它,通常有更好的解决方案。
我了解到您有两个函数需要使用相同的变量 channelCount
。为了避免全局,我会在 QwtPlotList
中保留对 channelCount
的引用,并将其作为参数传递给 addChannel
def addChannel(channelCount):
channelCount+=1
return channelCount
#QWtPlotList
#init
self.channelCount=3
def addChannel_2(self):
self.channelCount=addChannel(self.channelCount)
我让你想办法把所有东西放在一起:)