Python PyQt4类间信令

Python PyQt4类间信令,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,我有一系列类(基于相同的父类),它们是QTableWidget中的数据单元(因此它们都是从QItemDelegate派生的) 我试图创建一个信号,这些类可以传递给控制器,以传递数据更改 我找不到正确的组合(尽管进行了大量的实验和阅读)来实现。这是我的班级结构: 基类: class Criteria(QItemDelegate): def bind(self, update): self.connect(self,SIGNAL("criteriaChange(int, i

我有一系列类(基于相同的父类),它们是QTableWidget中的数据单元(因此它们都是从QItemDelegate派生的)

我试图创建一个信号,这些类可以传递给控制器,以传递数据更改

我找不到正确的组合(尽管进行了大量的实验和阅读)来实现。这是我的班级结构:

基类:

class Criteria(QItemDelegate):
    def bind(self, update):
        self.connect(self,SIGNAL("criteriaChange(int,  int, QVariant)"),update)    

    def emitCommitData(self):
        self.emit(SIGNAL("criteriaChange(int,  int, QVariant)"), self.Row, self.Col, self.getValue())
示例子类(如果需要更多信息,只需相关部分--LMK):

以下是我的硕士课程的相关部分:

@pyqtSlot(int,  int, QVariant,  name='criteriaChanged')
def setItem(self,  row,  col,  item):
    print row, col, item.toString()     # TODO:  Remove when tested
    self.Data[row][col] = item.toString()

def addCriteria(self, row, cname, ctype):
    self.setDirty()
    c = YesNo(cname, "YesNo")
    c.bind(self.setItem)

上面的代码给出了一个“基础C++对象已经被删除”。我试过这个:

def addCriteria(self, row, cname, ctype):
    self.setDirty()
    c = YesNo(cname, "YesNo")
    self.connect(c,SIGNAL("criteriaChange(int,  int, QVariant)"),self.setItem)
有什么建议吗?我不必使用这种方法,而是需要一种从单个控件中获取数据的方法

短暂性脑缺血发作

Mike

criteria.py

def criteria_change(row, col, new_value):
    print "change at", row, col, "new_value:", new_value

class Criteria:
    def __init__(self, row, col):
        self.row, self.col = row, col

    def on_change(self):
        criteria_change(self.row, self.col, self.value())
是的,也许不是

from PyQt4.QtGui import *

from criteria import Criteria

class YesNoMaybe(Criteria):
    def create_editor(self):
        group_box = QGroupBox()
        layout = QVBoxLayout()
        group_box.setLayout(layout)
        self.buttons = []

        for s in ["yes", "no", "maybe"]:
            button = QRadioButton(s)
            self.buttons.append(button)
            layout.addWidget(button)
            button.toggled.connect(self.on_toggle)

        return group_box
    #

    def on_toggle(self, is_now_on):
        if is_now_on:
            self.on_change()

    def value(self):
        for button in self.buttons:
            if button.isChecked():
                return button.text()
#
是的

from PyQt4.QtGui import QComboBox

from criteria import Criteria

class YesNo(Criteria):
    def create_editor(self):    
        combo_box = self.combo_box = QComboBox()
        for s in ["red", "blue"]:
            combo_box.addItem(s)
        combo_box.activated.connect(self.on_change) 
        return combo_box
    #

    def value(self):
        return self.combo_box.currentText()
#
main.py

import sys

from PyQt4.QtGui import *

from yes_no_maybe import YesNoMaybe
from yes_no import YesNo

app = QApplication(sys.argv)


table_classes = [[YesNo, YesNo],
                 [YesNoMaybe, YesNoMaybe]]  

table = QTableWidget(len(table_classes), len(table_classes[0]))
table.criteria = []
for r, cls_row in enumerate(table_classes):
    criteria_row = []
    table.criteria.append(criteria_row)
    for c, criteria_cls in enumerate(cls_row):
        criteria = criteria_cls(r, c)
        criteria_row.append(criteria)
        table.setCellWidget(r, c, criteria.create_editor())

table.setRowHeight(1, 100)
table.show()

app.exec_()
criteria.py

def criteria_change(row, col, new_value):
    print "change at", row, col, "new_value:", new_value

class Criteria:
    def __init__(self, row, col):
        self.row, self.col = row, col

    def on_change(self):
        criteria_change(self.row, self.col, self.value())
是的,也许不是

from PyQt4.QtGui import *

from criteria import Criteria

class YesNoMaybe(Criteria):
    def create_editor(self):
        group_box = QGroupBox()
        layout = QVBoxLayout()
        group_box.setLayout(layout)
        self.buttons = []

        for s in ["yes", "no", "maybe"]:
            button = QRadioButton(s)
            self.buttons.append(button)
            layout.addWidget(button)
            button.toggled.connect(self.on_toggle)

        return group_box
    #

    def on_toggle(self, is_now_on):
        if is_now_on:
            self.on_change()

    def value(self):
        for button in self.buttons:
            if button.isChecked():
                return button.text()
#
是的

from PyQt4.QtGui import QComboBox

from criteria import Criteria

class YesNo(Criteria):
    def create_editor(self):    
        combo_box = self.combo_box = QComboBox()
        for s in ["red", "blue"]:
            combo_box.addItem(s)
        combo_box.activated.connect(self.on_change) 
        return combo_box
    #

    def value(self):
        return self.combo_box.currentText()
#
main.py

import sys

from PyQt4.QtGui import *

from yes_no_maybe import YesNoMaybe
from yes_no import YesNo

app = QApplication(sys.argv)


table_classes = [[YesNo, YesNo],
                 [YesNoMaybe, YesNoMaybe]]  

table = QTableWidget(len(table_classes), len(table_classes[0]))
table.criteria = []
for r, cls_row in enumerate(table_classes):
    criteria_row = []
    table.criteria.append(criteria_row)
    for c, criteria_cls in enumerate(cls_row):
        criteria = criteria_cls(r, c)
        criteria_row.append(criteria)
        table.setCellWidget(r, c, criteria.create_editor())

table.setRowHeight(1, 100)
table.show()

app.exec_()

我真的很尴尬。希望这能帮助其他人

我没有调用相应对象的Qt初始化:

class YesNo(Criteria):
    def __init__(self,  name,  ctype):
        Criteria.__init__(self)            # <<<<----- This was missing before
        self.Name = name
        self.Index = ctype
类是否(标准):
定义初始化(self、name、ctype):

标准。我真的很尴尬。希望这能帮助其他人

我没有调用相应对象的Qt初始化:

class YesNo(Criteria):
    def __init__(self,  name,  ctype):
        Criteria.__init__(self)            # <<<<----- This was missing before
        self.Name = name
        self.Index = ctype
类是否(标准):
定义初始化(self、name、ctype):

标准.uuu init(self)#Jesse——这很好,但每个类都在一个单独的文件中。我试图使用Qt信号来跨越这些类。在我的示例中,您可以轻松地将这些类分解为单独的文件。我认为不需要Qt信号。我将编辑我的示例…Jesse--这很好,但每个类都在一个单独的文件中。我试图使用Qt信号来跨越这些类。在我的示例中,您可以轻松地将这些类分解为单独的文件。我认为不需要Qt信号。我将编辑我的示例…值得一提的是,我遇到了同样的错误,这很有帮助。值得一提的是,我遇到了同样的错误,这很有帮助。