如何知道 QTableWidget 的单元格是否正在(未)被编辑?
How to know whether QTableWidget's cell is (not) being edited?
我有一个 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)
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_())
一次只能编辑一个单元格。所以你只需要检查当前行是最后一行,并且 table 的 state is not in edit mode:
if (self.currentRow() == self.rowCount() - 1 and
self.state() != QtGui.QAbstractItemView.EditingState):
# add a new row
我有一个 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)
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_())
一次只能编辑一个单元格。所以你只需要检查当前行是最后一行,并且 table 的 state is not in edit mode:
if (self.currentRow() == self.rowCount() - 1 and
self.state() != QtGui.QAbstractItemView.EditingState):
# add a new row