2016-07-11 4 views
1

制限付き行のあるQTableWidgetがあります。最後の行でRETURNを押し、セルが編集されていない(編集モードに入るためにマウスをダブルクリックする)と、行を挿入するためにRETURNキーを押すようにしたいと思います。QTableWidgetのセルが編集されているかどうかを確認する方法?

セルが編集されていません。

enter image description here

コードは次のようである、私はFILLINする方法がわからない[現在のセルが編集されていません]:

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) 

答えて

2

一度に編集できるセルは1つだけです。だから、あなただけの現在の行が最後の行であることを確認する必要があり、そしてテーブルのstateedit modeではないこと:

if (self.currentRow() == self.rowCount() - 1 and 
     self.state() != QtGui.QAbstractItemView.EditingState): 
     # add a new row 
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_()) 
+1

何をどのようにQTableWidgetのセルが編集されていないかどうかを知ること」であるとタイトルを変更した場合'? – minion

+0

上にアップロードした写真のように、選択範囲の下に行がありません。ユーザーがRETURNキーを押してもう1行を挿入できるようにします。 – minion

関連する問題