Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent QTableView item from getting cleared on double-click

With QTableView set as editable using QAbstractTableModel's flag() method:

def flags(self, index):
    return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

double-clicking the QTableView's item puts this item into the editing mode. By default the pre-existing string disappears from the field and an entire item is blank. I wonder if this behavior can be avoided or overridden?

Here is the QTableView field before the user double-clicks it:

enter image description here

And here is how it looks on double-click:

enter image description here

EDITED WORKING CODE (Many thanks to M4rtini):

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os

class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items =[ 
                    ['Row0_Column0','Row0_Column1','Row0_Column2'],
                    ['Row1_Column0','Row1_Column1','Row1_Column2'],
                    ['Row2_Column0','Row2_Column1','Row2_Column2']
                    ]

    def flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

    def rowCount(self, parent):
        return len(self.items)       
    def columnCount(self, parent):
        return 3

    def data(self, index, role):
        if not index.isValid(): return QVariant()
        row=index.row()
        column=index.column()

        if row>len(self.items): return QVariant()        
        if column>len(self.items[row]): return QVariant()

        if role == Qt.EditRole or role == Qt.DisplayRole:
            return QVariant(self.items[row][column])

        return QVariant()

    def setData(self, index, value, role=Qt.EditRole):
        if index.isValid():            
            if role == Qt.EditRole:
                row = index.row()
                column=index.column()
                if row>len(self.items) or column>len(self.items[row]):
                    return False
                else:
                    self.items[row][column]=value  
                    return True
        return False

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        tablemodel=Model(self)               
        tableview=QTableView(self) 
        tableview.setModel(tablemodel)    
        layout=QVBoxLayout(self)
        layout.addWidget(tableview)
        self.setLayout(layout)      

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

like image 247
alphanumeric Avatar asked Jan 22 '15 05:01

alphanumeric


1 Answers

You need to sett the return value for data when called with EditRole:

def data(self, index, role):
    if not index.isValid(): return false
    row=index.row()
    if row>len(self.items): return false

    if role == Qt.DisplayRole or role == Qt.EditRole:
        return self.items[row]
like image 146
M4rtini Avatar answered Oct 22 '22 14:10

M4rtini