我的QTableWidget
行数有限。我想让用户按RETURN键插入一行,如果他们在最后一行按RETURN并且没有编辑单元格(双击鼠标进入编辑模式)。
正在编辑单元格:
代码是这样的,我不知道如何填充[当前单元格未被编辑]:
class MyTable(QTableWidget):
def KeyPressReturn(self, event):
if event.key() == Qt.Key_Return:
if .currentRow() is the last row] and [current cell is not being edited]:
insertRow(last_row_number)
答案 0 :(得分:2)
一次只能编辑一个单元格。所以你只需要检查当前行是最后一行,并且表state不在edit mode中:
if (self.currentRow() == self.rowCount() - 1 and
self.state() != QtGui.QAbstractItemView.EditingState):
# add a new row
答案 1 :(得分:0)
This is the one example to insert the new Column and Row. I think this you exception.
If your are not expecting this answer, sorry once again.
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.tableWidget = QtGui.QTableWidget(self)
self.tableWidget.setGeometry(QtCore.QRect(10, 20, 511, 192))
self.tableWidget.setObjectName('tableWidget')
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(20, 220, 101, 23))
self.pushButton.setObjectName('pushButton')
self.pushButton.setText('Add')
self.pushButton.clicked.connect (self.addItem)
self.tableWidget.cellClicked.connect (self.addLine)
def addItem (self) :
columnCount = self.tableWidget.columnCount ()
rowCount = self.tableWidget.rowCount ()
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem (columnCount+1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(rowCount+1, item)
self.tableWidget.setColumnCount(columnCount+1)
self.tableWidget.setRowCount(rowCount+1)
def addLine (self) :
rowCount = self.tableWidget.rowCount ()
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(rowCount+1, item)
self.tableWidget.setRowCount(rowCount+1)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())