我目前有一个QTableWidget与不同的单元格。当我左键单击一个单元格时,我希望它上面的数字增加1,如果我右键单击,我希望数字减少1。
对于第一部分,我使用
没有问题self.(widget_name).cellClicked.connect(self.addone)
右键单击会出现问题。有内置的方法吗?我怎样才能实现它?我尝试从QTableWidgets创建我自己的Table类并覆盖mousePressEvent但是没有工作(点击单元格没有做任何事情,它甚至没有选择单元格)。还尝试了eventFilter而没有。请参阅下文,了解我在两种情况下使用的确切代码。
对于mousePressEvent:
class Table(QtWidgets.QTableWidget):
def __init__(self,parent=None):
QtWidgets.QTableWidget.__init__(self,parent)
def mousePressEvent(self,QMouseEvent):
if QMouseEvent.button() == QtCore.LeftButton:
print('left') #FOR DEBUGGING
row = self.currentRow()
column = self.currentColumn()
new_text = round(float(self.item(row,column).text()) + 1,2)
self.item(row,column).setText(str(new_text))
elif event.button() == QtCore.RightButton:
print('right') #FOR DEBUGGING
row = self.currentRow()
column = self.currentColumn()
new_text = round(float(self.item(row,column).text()) - 1,2)
self.item(row,column).setText(str(new_text))
对于eventFilter:
class Table(QtWidgets.QTableWidget):
def __init__(self,parent=None):
QtWidgets.QTableWidget.__init__(self,parent)
self.installEventFilter(self)
def eventFilter(self,QObject,event):
if event.type() == QtCore.QEvent.MouseButtonPress:
if event.button() == QtCore.LeftButton:
print('left') #FOR DEBUGGING
row = self.currentRow()
column = self.currentColumn()
new_text = round(float(self.item(row,column).text()) + 1,2)
self.item(row,column).setText(str(new_text))
elif event.button() == QtCore.RightButton:
print('right') #FOR DEBUGGING
row = self.currentRow()
column = self.currentColumn()
new_text = round(float(self.item(row,column).text()) - 1,2)
self.item(row,column).setText(str(new_text))
两者(创建表格)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
self.centralwidget = QtWidgets.QWidget(self)
....
self.parameters = Table(self.centralwidget)
...
...
调试文本(打印“左”或“右”)也不会出现在控制台中。
答案 0 :(得分:2)
当你覆盖一个方法时,它总是调用父方法,因为它可以做其他类型的任务,你会省略它。另一方面,如果我们这样做,代表将被激活以编辑文本,这是不想要的,所以我们建立一个新的代表,没有发布者返回,如下所示:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class ReadOnlyDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, *args, **kwargs):
return
class Table(QtWidgets.QTableWidget):
def __init__(self, *args, **kwargs):
QtWidgets.QTableWidget.__init__(self, *args, **kwargs)
self.setItemDelegate(ReadOnlyDelegate(self))
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
it = self.itemAt(event.pos())
new_text = round(float(it.text()) + 1,2)
it.setText(str(new_text))
elif event.button() == QtCore.Qt.RightButton:
it = self.itemAt(event.pos())
new_text = round(float(it.text()) - 1,2)
it.setText(str(new_text))
QtWidgets.QTableWidget.mousePressEvent(self, event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
table = Table(5, 5)
self.setCentralWidget(table)
for i in range(5):
for j in range(5):
item = QtWidgets.QTableWidgetItem("{}".format(i*j))
table.setItem(i, j, item)
if __name__=='__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
答案 1 :(得分:1)
试一试:
class Table(QtWidgets.QTableWidget):
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print('left') # FOR DEBUGGING
row = self.currentRow()
column = self.currentColumn()
new_text = round(float(self.item(row,column).text()) + 1,2)
self.item(row,column).setText(str(new_text))
elif event.button() == QtCore.Qt.RightButton:
print('right') # FOR DEBUGGING
row = self.currentRow()
column = self.currentColumn()
new_text = round(float(self.item(row,column).text()) - 1,2)
self.item(row,column).setText(str(new_text))