minion
minion

Reputation: 571

How to know whether QTableWidget's cell is (not) being edited?

I have an QTableWidget with limited rows. I'd like let users to press RETURN to insert a row, if they press RETURN at the last row and no cell is being edited (double click the mouse to enter the editing mode).

Cell is not being edited:

enter image description here

The code is like this, I don't know how to fillin [current cell is not being edited]:

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)

Upvotes: 1

Views: 1081

Answers (2)

Subin Gopi
Subin Gopi

Reputation: 561

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_())

Upvotes: 0

ekhumoro
ekhumoro

Reputation: 120608

Only one cell can be edited at a time. So you just need to check that the current row is the last row, and that the table's state is not in edit mode:

    if (self.currentRow() == self.rowCount() - 1 and
        self.state() != QtGui.QAbstractItemView.EditingState):
        # add a new row

Upvotes: 3

Related Questions