如何连接到列代表的标志

2024-05-19 07:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为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_())

Tags: thefromimportselfnoneindexdef按钮

热门问题