如何连接到列代表的信号
How to connect to a column delegate's signal
我已经为 QTableView 创建了一个可用的 PushButton Delegate。当按下按钮时,它会发出一个 buttonClicked 信号(同时还会打印以表明它正在工作。
我已将按钮委托设置为第 1 列的项目委托(请参阅接近末尾的 tableview.setItemDelegateForColumn(1, 委托)。
我被卡住的地方是我想知道如何连接到按钮发射信号。作为初学者,我可以遍历我的 rows/columns 并找到 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_())
一个委托实例为所有列发出 buttonClicked
信号,因此您只需要做:
delegate = PushButtonDelegate(None)
delegate.connect(slot)
我已经为 QTableView 创建了一个可用的 PushButton Delegate。当按下按钮时,它会发出一个 buttonClicked 信号(同时还会打印以表明它正在工作。
我已将按钮委托设置为第 1 列的项目委托(请参阅接近末尾的 tableview.setItemDelegateForColumn(1, 委托)。
我被卡住的地方是我想知道如何连接到按钮发射信号。作为初学者,我可以遍历我的 rows/columns 并找到 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_())
一个委托实例为所有列发出 buttonClicked
信号,因此您只需要做:
delegate = PushButtonDelegate(None)
delegate.connect(slot)