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

1

9

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

Piscatorial answered 22/1, 2015 at 5:44 Comment(1)
What python\pyqt version are you using? My version throws a lot of errors\warnings from all those QVariants Any Python object may be used whenever a QVariant is expectedBerti
B
12

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]
Berti answered 22/1, 2015 at 6:0 Comment(1)
@Sputnix sine you're returning the same text for both roles, you should do it under the same if statement. Leads to less duplicated code :)Berti

© 2022 - 2024 — McMap. All rights reserved.