PyQt4 类之间的信号传递

发布于 2024-08-17 06:38:55 字数 1515 浏览 2 评论 0原文

我有一个类系列(基于相同的父类),它们是 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):

class YesNo(Criteria):
    ....
    def createEditor(self, parent, option, index):
        self.comboBox = QComboBox(parent)
        for item in self.getChoices():
            self.comboBox.addItem(item)
        self.comboBox.activated.connect(self.emitCommitData) 
        return self.comboBox
    ....

这是我的主类的相关部分:

@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)

有什么建议吗?我不必使用此方法,而是需要一种从各个控件中获取数据的方法。

蒂亚·

迈克

I have a family of classes (based on the same parent class) that are data cells in a QTableWidget (so they are all derived from QItemDelegate).

I'm trying to create a signal that these classes can pass up to the controller to communicate data changes.

I can't find the right combination (despite much experimentation and reading) which accomplished. Here's my class structure:

Base Class:

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())

Example Sub-class (just the relevant parts -- LMK if more info needed):

class YesNo(Criteria):
    ....
    def createEditor(self, parent, option, index):
        self.comboBox = QComboBox(parent)
        for item in self.getChoices():
            self.comboBox.addItem(item)
        self.comboBox.activated.connect(self.emitCommitData) 
        return self.comboBox
    ....

Here's the relevant portion of my master class:

@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)

The above code gives an "Underlying C++ object has been deleted". I've tried this:

def addCriteria(self, row, cname, ctype):
    self.setDirty()
    c = YesNo(cname, "YesNo")
    self.connect(c,SIGNAL("criteriaChange(int,  int, QVariant)"),self.setItem)

Any suggestions? I don't have to use this method, but rather need a way to get that data out of the individual controls.

TIA

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人如初 2024-08-24 06:38:55

对此我真的很尴尬。希望这会帮助其他人。

我没有为适当的对象调用 Qt 初始化:

class YesNo(Criteria):
    def __init__(self,  name,  ctype):
        Criteria.__init__(self)            # <<<<----- This was missing before
        self.Name = name
        self.Index = ctype

并且

class Criteria(QItemDelegate):
    def __init__(self):
        QItemDelegate.__init__(self)       # <<<<----- This was missing before

I'm really embarrassed about this. Hopefully this will help somebody else.

I didn't call the Qt initialization for the appropriate object:

class YesNo(Criteria):
    def __init__(self,  name,  ctype):
        Criteria.__init__(self)            # <<<<----- This was missing before
        self.Name = name
        self.Index = ctype

and

class Criteria(QItemDelegate):
    def __init__(self):
        QItemDelegate.__init__(self)       # <<<<----- This was missing before
葬花如无物 2024-08-24 06:38:55

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())

yes_no_maybe.py

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()
#

yes_no.py

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())

yes_no_maybe.py

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()
#

yes_no.py

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_()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文