Python 如何连接到列委托';s信号

Python 如何连接到列委托';s信号,python,python-3.x,pyqt5,qt-signals,qstyleditemdelegate,Python,Python 3.x,Pyqt5,Qt Signals,Qstyleditemdelegate,我已经为QTableView创建了一个工作按钮委托。当按下按钮时,它将发出按钮点击信号(并打印以显示其正在工作) 我已将按钮委托设置为第1列的项目委托(请参见tableview.setItemDelegateForColumn(1,委托),靠近末尾 我被卡住的地方是我想知道如何连接到按钮发射信号。作为启动程序,我可以循环我的行/列,并在tableView中找到每个元素(您可以在最后的示例中看到),所以我想这可能是我可以建立连接的点。但是,我不确定如何进行连接。可以在列级别进行连接吗 任何帮助都将

我已经为QTableView创建了一个工作按钮委托。当按下按钮时,它将发出按钮点击信号(并打印以显示其正在工作)

我已将按钮委托设置为第1列的项目委托(请参见tableview.setItemDelegateForColumn(1,委托),靠近末尾

我被卡住的地方是我想知道如何连接到按钮发射信号。作为启动程序,我可以循环我的行/列,并在tableView中找到每个元素(您可以在最后的示例中看到),所以我想这可能是我可以建立连接的点。但是,我不确定如何进行连接。可以在列级别进行连接吗

任何帮助都将不胜感激

# python 3.6
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QApplication, QStyleOptionButton, QTableView, QStyledItemDelegate, QStyle)
from PyQt5.QtCore import QModelIndex


class PushButtonDelegate(QStyledItemDelegate):

    buttonClicked = QtCore.pyqtSignal(int, int)

    def __init__(self, parent = None):
    super().__init__(parent)
    self._pressed = None
    self._buttonTxt = 'Press Me'

    def paint(self, painter, option, index):
        painter.save()
        opt = QStyleOptionButton()

        opt.text = self._buttonTxt
        opt.rect = option.rect
        opt.palette = option.palette
        if self._pressed and self._pressed == (index.row(), index.column()):
            opt.state = QStyle.State_Enabled | QStyle.State_Sunken
        else:
            opt.state = QStyle.State_Enabled | QStyle.State_Raised
        QtWidgets.QApplication.style().drawControl(QStyle.CE_PushButton, opt, painter)
        painter.restore()

    def editorEvent(self, event, model, option, index):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            # store the position that is clicked
            self._pressed = (index.row(), index.column())
            return True
        elif event.type() == QtCore.QEvent.MouseButtonRelease:
            if self._pressed == (index.row(), index.column()):
                print("Button pressed at {} {}".format(index.row(), index.column()))
                self.buttonClicked.emit(*self._pressed)
            self._pressed = None
            return True

        return False

    def createEditor(self, parent, option, index):
        """ Disable the createEditor or you'll lose your button on a double-click """
        return None

    def setEditorData(self, item, index):
        """ We don't change what's in the button so disable this event also """
        return None

def callBack_test(row, col):
    print("CallbackTest called with {} {}".format(row, col))

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)

    rows = 4
    cols = 2
    delegate_col = 1

    # Create an table view with a bunch of rows and columsn
    model = QtGui.QStandardItemModel(rows, cols)
    tableView = QTableView()
    tableView.setWindowTitle("Pushbutton Delegate example")
    tableView.setModel(model)

    # We are setting column 1 to use the push button widget
    delegate = PushButtonDelegate(None)
    delegate.buttonClicked.connect(callBack_test)  #<-- This is the answer
    tableView.setItemDelegateForColumn(delegate_col, delegate)

    # Now lets loop through our button delegates and connect to the signal?
    for row in range(rows):
        index = model.index(row, delegate_col, QModelIndex())
        model.setData(index, 1)
        # Is it possible to connect to the pushbutton signal here?


    tableView.show()

    sys.exit(app.exec_())
#python 3.6
从PyQt5导入QtCore
从PyQt5导入QtGui
从PyQt5导入QtWidgets
从PyQt5.qtwidts导入(QApplication、QStyleOptionButton、QTableView、QStyledItemDelegate、QStyle)
从PyQt5.QtCore导入QModelIndex
PushButtonDelegate类(QStyledItemDelegate):
buttonClicked=QtCore.pyqtSignal(int,int)
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
按下self.\u=无
self.\u buttonText='按我'
def油漆(自身、油漆工、选项、索引):
保存
opt=QStyleOptionButton()
opt.text=self.\u按钮text
opt.rect=option.rect
opt.palete=option.palete
如果按下self.\u并按下self.\u==(index.row(),index.column()):
opt.state=QStyle.state_启用| QStyle.state_凹陷
其他:
opt.state=QStyle.state_已启用| QStyle.state_已引发
qtwidts.QApplication.style().drawControl(QStyle.CE_按钮、选项、画师)
恢复
def editorEvent(自身、事件、模型、选项、索引):
如果event.type()==QtCore.QEvent.mousebutton按下:
#存储单击的位置
self.\u pressed=(index.row(),index.column())
返回真值
elif event.type()==QtCore.QEvent.MouseButtonRelease:
如果按下self.\u==(index.row(),index.column()):
打印(“在{}{}按下按钮”。格式(index.row(),index.column())
self.button点击发射(*self.\u按下)
按下self.\u=无
返回真值
返回错误
def createEditor(自身、父项、选项、索引):
“”“禁用createEditor,否则双击时将丢失按钮”“”
一无所获
def setEditorData(自身、项目、索引):
“”“我们不更改按钮中的内容,因此也禁用此事件”“”
一无所获
def回调_测试(行、列):
打印(“用{}{}调用的回调测试”。格式(行,列))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
导入系统
app=QApplication(sys.argv)
行数=4
cols=2
代表_col=1
#创建一个包含一组行和列的表视图
model=QtGui.QStandardItemModel(行、列)
tableView=QTableView()
tableView.setWindowTitle(“按钮代理示例”)
tableView.setModel(模型)
#我们将第1列设置为使用按钮小部件
委派=按钮委派(无)

delegate.buttonClicked.connect(callBack#u test)一个委托实例为所有列发出
buttonClicked
信号,因此您只需执行以下操作:

delegate = PushButtonDelegate(None)
delegate.connect(slot)

一个代理为所有列发出
buttonClicked
信号,因此您只需执行
delegate.connect(slot)
(PS:当拖动按钮时,您的鼠标操作将不起作用)。@ekhumoro Ok好极了。上面已使用工作示例delegate.buttonClicked.connect进行了更新(callBack\u test)。发布你的答案,我会接受。另外,关于鼠标操作,它可以按照我的要求工作。如果他们点击并拖出单元格,则不会发生任何事件。好的,完成。(我的意思是鼠标操作是,如果你在外拖动并释放,按钮将保持向下)。我明白你的意思。谢谢