Issue with QTableView Edit Mode










1















I've subclassed a QStandardItemModel:



class PPSTableEditModel(QStandardItemModel):

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel()
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")
self.editmode = None
self.history =

def edit_record(self, qndx):

#qndx is passed from a QTableView class and contains view.currentIndex()

self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
self.process_record(qndx) #<---- This works

#ix = self.index(qndx.row(),2)
#self.process_record(ix) #<---- This causes the error

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


The issue I am having is from the edit_record method I pass the qndxto process_record and the view goes into edit mode properly; however, if I attempt to set the starting (by reassigning the qndx) column I get an error:



edit: index was invalid
edit: editing failed


* EDIT *
Attempted to map to the proxy index for editing and I am still getting the same error:



def edit_record(self, qndx):
self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)


* EDIT *
MCVE:



import uuid,sys
from PyQt5.QtSql import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
query = QSqlQuery()
query.prepare('SELECT * FROM scldata WHERE svcdataid=?')
query.addBindValue(parentid)
query.exec_()
while query.next():
record =
for ndx in range(0, query.record().count()):
item = QStandardItem()
item.setData(query.value(ndx), Qt.DisplayRole)
record.append(item)
record.append(QStandardItem("0"))
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)


app = QApplication(sys.argv)
mainWin = SclDataBrowse()
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("myhost")
db.setUserName("myusr")
db.setPassword("mypassword")
db.setDatabaseName("mydbname")
db.open()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


* EDIT *
Seems to be something with the QSortFilterProxyModel though I can't pin it down. Passing the index to process_record without mapping it through the proxy allows me to edit but the underlying data is incorrect.



*** EDIT ***
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *


class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)
#ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
#self.process_record(ix)

def delete_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.item(ix.row(),5).setData("1",Qt.DisplayRole)

def process_record(self, pndx):
for ndx in range(0,self.columnCount()):
print('0,1----->2'.format(pndx.row(),ndx,self.item(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
data = (
"recordid": "1", "svcdataid": 1, "samount": .01, "eamount": 5000, "rate": .065, "delflag": "0",
"recordid": "2", "svcdataid": 1, "samount": 5000.01, "eamount": 10000, "rate": .065, "delflag": "0",
"recordid": "3", "svcdataid": 1, "samount": 10000.01, "eamount": 15000, "rate": .060, "delflag": "0",
"recordid": "4", "svcdataid": 1, "samount": 15000.01, "eamount": 20000, "rate": .055, "delflag": "0",
"recordid": "5", "svcdataid": 1, "samount": 20000.01, "eamount": 99999, "rate": .05, "delflag": "0"
)
for ndx,rec in enumerate(data):
record =
for key in rec:
item = QStandardItem()
item.setData(rec[key], Qt.DisplayRole)
record.append(item)
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.btn_layout = QHBoxLayout()
self.btn_layout.setContentsMargins(6, 6, 6, 6)
self.btn_delete = QPushButton()
self.btn_delete.setText("Delete")
self.btn_layout.addWidget(self.btn_delete)
self.view_layout.addLayout(self.btn_layout)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.btn_delete.clicked.connect(self.delete_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)

def delete_record(self):
qndx = self.tbl_View.currentIndex()
self.model.delete_record(qndx)



app = QApplication(sys.argv)
mainWin = SclDataBrowse()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


With this code, there are 5 entries in the table. If you delete, say, the second record the filters correctly hides the "deleted" record. Now double click on the 3rd or 4th row and the data outputted in process_record reports the correct row but the data associated with that row is incorrect.










share|improve this question
























  • please provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Nov 12 '18 at 18:58











  • MCVE posted. Thanks Eyllanesc,

    – Elcid_91
    Nov 12 '18 at 19:15











  • please there are many things that are not defined by example: btn_New, SclDataDelegate, etc, please correct your MCVE adding also the imports and the main.

    – eyllanesc
    Nov 12 '18 at 19:18












  • Stripped it down a little more....see if this works.

    – Elcid_91
    Nov 12 '18 at 19:43















1















I've subclassed a QStandardItemModel:



class PPSTableEditModel(QStandardItemModel):

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel()
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")
self.editmode = None
self.history =

def edit_record(self, qndx):

#qndx is passed from a QTableView class and contains view.currentIndex()

self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
self.process_record(qndx) #<---- This works

#ix = self.index(qndx.row(),2)
#self.process_record(ix) #<---- This causes the error

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


The issue I am having is from the edit_record method I pass the qndxto process_record and the view goes into edit mode properly; however, if I attempt to set the starting (by reassigning the qndx) column I get an error:



edit: index was invalid
edit: editing failed


* EDIT *
Attempted to map to the proxy index for editing and I am still getting the same error:



def edit_record(self, qndx):
self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)


* EDIT *
MCVE:



import uuid,sys
from PyQt5.QtSql import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
query = QSqlQuery()
query.prepare('SELECT * FROM scldata WHERE svcdataid=?')
query.addBindValue(parentid)
query.exec_()
while query.next():
record =
for ndx in range(0, query.record().count()):
item = QStandardItem()
item.setData(query.value(ndx), Qt.DisplayRole)
record.append(item)
record.append(QStandardItem("0"))
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)


app = QApplication(sys.argv)
mainWin = SclDataBrowse()
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("myhost")
db.setUserName("myusr")
db.setPassword("mypassword")
db.setDatabaseName("mydbname")
db.open()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


* EDIT *
Seems to be something with the QSortFilterProxyModel though I can't pin it down. Passing the index to process_record without mapping it through the proxy allows me to edit but the underlying data is incorrect.



*** EDIT ***
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *


class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)
#ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
#self.process_record(ix)

def delete_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.item(ix.row(),5).setData("1",Qt.DisplayRole)

def process_record(self, pndx):
for ndx in range(0,self.columnCount()):
print('0,1----->2'.format(pndx.row(),ndx,self.item(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
data = (
"recordid": "1", "svcdataid": 1, "samount": .01, "eamount": 5000, "rate": .065, "delflag": "0",
"recordid": "2", "svcdataid": 1, "samount": 5000.01, "eamount": 10000, "rate": .065, "delflag": "0",
"recordid": "3", "svcdataid": 1, "samount": 10000.01, "eamount": 15000, "rate": .060, "delflag": "0",
"recordid": "4", "svcdataid": 1, "samount": 15000.01, "eamount": 20000, "rate": .055, "delflag": "0",
"recordid": "5", "svcdataid": 1, "samount": 20000.01, "eamount": 99999, "rate": .05, "delflag": "0"
)
for ndx,rec in enumerate(data):
record =
for key in rec:
item = QStandardItem()
item.setData(rec[key], Qt.DisplayRole)
record.append(item)
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.btn_layout = QHBoxLayout()
self.btn_layout.setContentsMargins(6, 6, 6, 6)
self.btn_delete = QPushButton()
self.btn_delete.setText("Delete")
self.btn_layout.addWidget(self.btn_delete)
self.view_layout.addLayout(self.btn_layout)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.btn_delete.clicked.connect(self.delete_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)

def delete_record(self):
qndx = self.tbl_View.currentIndex()
self.model.delete_record(qndx)



app = QApplication(sys.argv)
mainWin = SclDataBrowse()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


With this code, there are 5 entries in the table. If you delete, say, the second record the filters correctly hides the "deleted" record. Now double click on the 3rd or 4th row and the data outputted in process_record reports the correct row but the data associated with that row is incorrect.










share|improve this question
























  • please provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Nov 12 '18 at 18:58











  • MCVE posted. Thanks Eyllanesc,

    – Elcid_91
    Nov 12 '18 at 19:15











  • please there are many things that are not defined by example: btn_New, SclDataDelegate, etc, please correct your MCVE adding also the imports and the main.

    – eyllanesc
    Nov 12 '18 at 19:18












  • Stripped it down a little more....see if this works.

    – Elcid_91
    Nov 12 '18 at 19:43













1












1








1








I've subclassed a QStandardItemModel:



class PPSTableEditModel(QStandardItemModel):

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel()
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")
self.editmode = None
self.history =

def edit_record(self, qndx):

#qndx is passed from a QTableView class and contains view.currentIndex()

self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
self.process_record(qndx) #<---- This works

#ix = self.index(qndx.row(),2)
#self.process_record(ix) #<---- This causes the error

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


The issue I am having is from the edit_record method I pass the qndxto process_record and the view goes into edit mode properly; however, if I attempt to set the starting (by reassigning the qndx) column I get an error:



edit: index was invalid
edit: editing failed


* EDIT *
Attempted to map to the proxy index for editing and I am still getting the same error:



def edit_record(self, qndx):
self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)


* EDIT *
MCVE:



import uuid,sys
from PyQt5.QtSql import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
query = QSqlQuery()
query.prepare('SELECT * FROM scldata WHERE svcdataid=?')
query.addBindValue(parentid)
query.exec_()
while query.next():
record =
for ndx in range(0, query.record().count()):
item = QStandardItem()
item.setData(query.value(ndx), Qt.DisplayRole)
record.append(item)
record.append(QStandardItem("0"))
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)


app = QApplication(sys.argv)
mainWin = SclDataBrowse()
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("myhost")
db.setUserName("myusr")
db.setPassword("mypassword")
db.setDatabaseName("mydbname")
db.open()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


* EDIT *
Seems to be something with the QSortFilterProxyModel though I can't pin it down. Passing the index to process_record without mapping it through the proxy allows me to edit but the underlying data is incorrect.



*** EDIT ***
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *


class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)
#ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
#self.process_record(ix)

def delete_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.item(ix.row(),5).setData("1",Qt.DisplayRole)

def process_record(self, pndx):
for ndx in range(0,self.columnCount()):
print('0,1----->2'.format(pndx.row(),ndx,self.item(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
data = (
"recordid": "1", "svcdataid": 1, "samount": .01, "eamount": 5000, "rate": .065, "delflag": "0",
"recordid": "2", "svcdataid": 1, "samount": 5000.01, "eamount": 10000, "rate": .065, "delflag": "0",
"recordid": "3", "svcdataid": 1, "samount": 10000.01, "eamount": 15000, "rate": .060, "delflag": "0",
"recordid": "4", "svcdataid": 1, "samount": 15000.01, "eamount": 20000, "rate": .055, "delflag": "0",
"recordid": "5", "svcdataid": 1, "samount": 20000.01, "eamount": 99999, "rate": .05, "delflag": "0"
)
for ndx,rec in enumerate(data):
record =
for key in rec:
item = QStandardItem()
item.setData(rec[key], Qt.DisplayRole)
record.append(item)
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.btn_layout = QHBoxLayout()
self.btn_layout.setContentsMargins(6, 6, 6, 6)
self.btn_delete = QPushButton()
self.btn_delete.setText("Delete")
self.btn_layout.addWidget(self.btn_delete)
self.view_layout.addLayout(self.btn_layout)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.btn_delete.clicked.connect(self.delete_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)

def delete_record(self):
qndx = self.tbl_View.currentIndex()
self.model.delete_record(qndx)



app = QApplication(sys.argv)
mainWin = SclDataBrowse()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


With this code, there are 5 entries in the table. If you delete, say, the second record the filters correctly hides the "deleted" record. Now double click on the 3rd or 4th row and the data outputted in process_record reports the correct row but the data associated with that row is incorrect.










share|improve this question
















I've subclassed a QStandardItemModel:



class PPSTableEditModel(QStandardItemModel):

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel()
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")
self.editmode = None
self.history =

def edit_record(self, qndx):

#qndx is passed from a QTableView class and contains view.currentIndex()

self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
self.process_record(qndx) #<---- This works

#ix = self.index(qndx.row(),2)
#self.process_record(ix) #<---- This causes the error

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


The issue I am having is from the edit_record method I pass the qndxto process_record and the view goes into edit mode properly; however, if I attempt to set the starting (by reassigning the qndx) column I get an error:



edit: index was invalid
edit: editing failed


* EDIT *
Attempted to map to the proxy index for editing and I am still getting the same error:



def edit_record(self, qndx):
self.editmode = 2
del self.history[:]
for ndx in range(0, self.columnCount()):
self.history.append(self.item(qndx.row(),ndx).data(Qt.DisplayRole))
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)


* EDIT *
MCVE:



import uuid,sys
from PyQt5.QtSql import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.process_record(ix)

def process_record(self, pndx):
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
query = QSqlQuery()
query.prepare('SELECT * FROM scldata WHERE svcdataid=?')
query.addBindValue(parentid)
query.exec_()
while query.next():
record =
for ndx in range(0, query.record().count()):
item = QStandardItem()
item.setData(query.value(ndx), Qt.DisplayRole)
record.append(item)
record.append(QStandardItem("0"))
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)


app = QApplication(sys.argv)
mainWin = SclDataBrowse()
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("myhost")
db.setUserName("myusr")
db.setPassword("mypassword")
db.setDatabaseName("mydbname")
db.open()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


* EDIT *
Seems to be something with the QSortFilterProxyModel though I can't pin it down. Passing the index to process_record without mapping it through the proxy allows me to edit but the underlying data is incorrect.



*** EDIT ***
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *


class PPSTableEditModel(QStandardItemModel):
model_loaded = pyqtSignal()

def __init__(self, parent=None):
super(PPSTableEditModel, self).__init__(parent)
self.proxy = None

def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)
#ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
#self.process_record(ix)

def delete_record(self, qndx):
ix = qndx if not self.proxy else self.proxy.mapToSource(qndx)
self.item(ix.row(),5).setData("1",Qt.DisplayRole)

def process_record(self, pndx):
for ndx in range(0,self.columnCount()):
print('0,1----->2'.format(pndx.row(),ndx,self.item(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)


class SclDataModel(PPSTableEditModel):

def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self)
self.proxy.setFilterKeyColumn(5)
self.proxy.setDynamicSortFilter(True)
self.proxy.setFilterFixedString("0")

def load_data(self, parentid):
data = (
"recordid": "1", "svcdataid": 1, "samount": .01, "eamount": 5000, "rate": .065, "delflag": "0",
"recordid": "2", "svcdataid": 1, "samount": 5000.01, "eamount": 10000, "rate": .065, "delflag": "0",
"recordid": "3", "svcdataid": 1, "samount": 10000.01, "eamount": 15000, "rate": .060, "delflag": "0",
"recordid": "4", "svcdataid": 1, "samount": 15000.01, "eamount": 20000, "rate": .055, "delflag": "0",
"recordid": "5", "svcdataid": 1, "samount": 20000.01, "eamount": 99999, "rate": .05, "delflag": "0"
)
for ndx,rec in enumerate(data):
record =
for key in rec:
item = QStandardItem()
item.setData(rec[key], Qt.DisplayRole)
record.append(item)
self.insertRow(self.rowCount(), record)
self.model_loaded.emit()


class SclDataBrowse(QWidget):
def __init__(self, parent=None):
super(SclDataBrowse, self).__init__(parent)
self.resize(600, 297)
self.tbl_View = QTableView(self)
self.line = QFrame(self)
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.view_layout = QVBoxLayout(self)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.addWidget(self.tbl_View)
self.view_layout.addWidget(self.line)
self.btn_layout = QHBoxLayout()
self.btn_layout.setContentsMargins(6, 6, 6, 6)
self.btn_delete = QPushButton()
self.btn_delete.setText("Delete")
self.btn_layout.addWidget(self.btn_delete)
self.view_layout.addLayout(self.btn_layout)
self.model = SclDataModel(self)
self.parentid = None
self.setup_view()
self.setup_connections()

def setup_view(self):
self.tbl_View.setModel(self.model.proxy)
self.tbl_View.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tbl_View.setSelectionMode(QAbstractItemView.SingleSelection)
self.tbl_View.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tbl_View.verticalHeader().setVisible(False)
self.tbl_View.verticalHeader().setDefaultSectionSize(22)

def setup_connections(self):
self.tbl_View.doubleClicked.connect(self.edit_record)
self.btn_delete.clicked.connect(self.delete_record)
self.model.model_loaded.connect(self.format_view)

def set_parent(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def format_view(self):
if self.model.rowCount():
header = ["recorid", "svcdataid", "Start Amount", "End Amount", "Rate", "deleteflag"]
for n in range(0, len(header)):
self.model.setHeaderData(n, Qt.Horizontal, header[n])
self.tbl_View.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.tbl_View.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.tbl_View.setColumnWidth(4, 85)
self.tbl_View.setColumnHidden(0, True)
self.tbl_View.setColumnHidden(1, True)
self.tbl_View.setColumnHidden(5, True)

def set_parent_record(self, parentid):
self.parentid = parentid
self.model.load_data(self.parentid)

def edit_record(self):
qndx = self.tbl_View.currentIndex()
self.model.edit_record(qndx)

def delete_record(self):
qndx = self.tbl_View.currentIndex()
self.model.delete_record(qndx)



app = QApplication(sys.argv)
mainWin = SclDataBrowse()
mainWin.show()
mainWin.set_parent("a5865717-e125-11e8-80c0-d4ae52cc00a8")
sys.exit(app.exec_())


With this code, there are 5 entries in the table. If you delete, say, the second record the filters correctly hides the "deleted" record. Now double click on the 3rd or 4th row and the data outputted in process_record reports the correct row but the data associated with that row is incorrect.







python python-3.x pyqt pyqt5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 18:32







Elcid_91

















asked Nov 12 '18 at 18:29









Elcid_91Elcid_91

4911722




4911722












  • please provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Nov 12 '18 at 18:58











  • MCVE posted. Thanks Eyllanesc,

    – Elcid_91
    Nov 12 '18 at 19:15











  • please there are many things that are not defined by example: btn_New, SclDataDelegate, etc, please correct your MCVE adding also the imports and the main.

    – eyllanesc
    Nov 12 '18 at 19:18












  • Stripped it down a little more....see if this works.

    – Elcid_91
    Nov 12 '18 at 19:43

















  • please provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Nov 12 '18 at 18:58











  • MCVE posted. Thanks Eyllanesc,

    – Elcid_91
    Nov 12 '18 at 19:15











  • please there are many things that are not defined by example: btn_New, SclDataDelegate, etc, please correct your MCVE adding also the imports and the main.

    – eyllanesc
    Nov 12 '18 at 19:18












  • Stripped it down a little more....see if this works.

    – Elcid_91
    Nov 12 '18 at 19:43
















please provide a Minimal, Complete, and Verifiable example

– eyllanesc
Nov 12 '18 at 18:58





please provide a Minimal, Complete, and Verifiable example

– eyllanesc
Nov 12 '18 at 18:58













MCVE posted. Thanks Eyllanesc,

– Elcid_91
Nov 12 '18 at 19:15





MCVE posted. Thanks Eyllanesc,

– Elcid_91
Nov 12 '18 at 19:15













please there are many things that are not defined by example: btn_New, SclDataDelegate, etc, please correct your MCVE adding also the imports and the main.

– eyllanesc
Nov 12 '18 at 19:18






please there are many things that are not defined by example: btn_New, SclDataDelegate, etc, please correct your MCVE adding also the imports and the main.

– eyllanesc
Nov 12 '18 at 19:18














Stripped it down a little more....see if this works.

– Elcid_91
Nov 12 '18 at 19:43





Stripped it down a little more....see if this works.

– Elcid_91
Nov 12 '18 at 19:43












1 Answer
1






active

oldest

votes


















1














According to your code, you want to access an index that is in the same row but in the second column, that is, it is a sibling of the QModelIndex, so you must use the sibling function:



def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)



The problem is because you are using the item to obtain the data, QTableView does not know the k, it only knows the model that has been established, that is, the QSortFilterProxyModel, so the QModelIndex that requires the edit() and selectionModel().SetCurrentIndex() must be the QSortFilterProxyModel so that's why my initial solution works, but in your impression you are using the PPSTableEditModel model so the solution is to use the proxy but it is not necessary to store it since the QModelIndex saves a model reference to belongs:



def process_record(self, pndx):
model = pndx.model()
for ndx in range(model.columnCount()):
print('0,1----->2'.format(pndx.row(), ndx, model.index(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)





share|improve this answer

























  • Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

    – Elcid_91
    Nov 13 '18 at 0:55












  • @Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

    – eyllanesc
    Nov 13 '18 at 1:13











  • Made another edit to exemplify the issue. Thanks eyllanesc.

    – Elcid_91
    Nov 13 '18 at 18:35











  • Thanks eyllanesc. I now understand the modeling. Cheers!

    – Elcid_91
    Nov 16 '18 at 13:49










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268053%2fissue-with-qtableview-edit-mode%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














According to your code, you want to access an index that is in the same row but in the second column, that is, it is a sibling of the QModelIndex, so you must use the sibling function:



def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)



The problem is because you are using the item to obtain the data, QTableView does not know the k, it only knows the model that has been established, that is, the QSortFilterProxyModel, so the QModelIndex that requires the edit() and selectionModel().SetCurrentIndex() must be the QSortFilterProxyModel so that's why my initial solution works, but in your impression you are using the PPSTableEditModel model so the solution is to use the proxy but it is not necessary to store it since the QModelIndex saves a model reference to belongs:



def process_record(self, pndx):
model = pndx.model()
for ndx in range(model.columnCount()):
print('0,1----->2'.format(pndx.row(), ndx, model.index(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)





share|improve this answer

























  • Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

    – Elcid_91
    Nov 13 '18 at 0:55












  • @Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

    – eyllanesc
    Nov 13 '18 at 1:13











  • Made another edit to exemplify the issue. Thanks eyllanesc.

    – Elcid_91
    Nov 13 '18 at 18:35











  • Thanks eyllanesc. I now understand the modeling. Cheers!

    – Elcid_91
    Nov 16 '18 at 13:49















1














According to your code, you want to access an index that is in the same row but in the second column, that is, it is a sibling of the QModelIndex, so you must use the sibling function:



def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)



The problem is because you are using the item to obtain the data, QTableView does not know the k, it only knows the model that has been established, that is, the QSortFilterProxyModel, so the QModelIndex that requires the edit() and selectionModel().SetCurrentIndex() must be the QSortFilterProxyModel so that's why my initial solution works, but in your impression you are using the PPSTableEditModel model so the solution is to use the proxy but it is not necessary to store it since the QModelIndex saves a model reference to belongs:



def process_record(self, pndx):
model = pndx.model()
for ndx in range(model.columnCount()):
print('0,1----->2'.format(pndx.row(), ndx, model.index(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)





share|improve this answer

























  • Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

    – Elcid_91
    Nov 13 '18 at 0:55












  • @Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

    – eyllanesc
    Nov 13 '18 at 1:13











  • Made another edit to exemplify the issue. Thanks eyllanesc.

    – Elcid_91
    Nov 13 '18 at 18:35











  • Thanks eyllanesc. I now understand the modeling. Cheers!

    – Elcid_91
    Nov 16 '18 at 13:49













1












1








1







According to your code, you want to access an index that is in the same row but in the second column, that is, it is a sibling of the QModelIndex, so you must use the sibling function:



def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)



The problem is because you are using the item to obtain the data, QTableView does not know the k, it only knows the model that has been established, that is, the QSortFilterProxyModel, so the QModelIndex that requires the edit() and selectionModel().SetCurrentIndex() must be the QSortFilterProxyModel so that's why my initial solution works, but in your impression you are using the PPSTableEditModel model so the solution is to use the proxy but it is not necessary to store it since the QModelIndex saves a model reference to belongs:



def process_record(self, pndx):
model = pndx.model()
for ndx in range(model.columnCount()):
print('0,1----->2'.format(pndx.row(), ndx, model.index(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)





share|improve this answer















According to your code, you want to access an index that is in the same row but in the second column, that is, it is a sibling of the QModelIndex, so you must use the sibling function:



def edit_record(self, qndx):
ix = qndx.sibling(qndx.row(), 2)
self.process_record(ix)



The problem is because you are using the item to obtain the data, QTableView does not know the k, it only knows the model that has been established, that is, the QSortFilterProxyModel, so the QModelIndex that requires the edit() and selectionModel().SetCurrentIndex() must be the QSortFilterProxyModel so that's why my initial solution works, but in your impression you are using the PPSTableEditModel model so the solution is to use the proxy but it is not necessary to store it since the QModelIndex saves a model reference to belongs:



def process_record(self, pndx):
model = pndx.model()
for ndx in range(model.columnCount()):
print('0,1----->2'.format(pndx.row(), ndx, model.index(pndx.row(),ndx).data(Qt.DisplayRole)))
self.parent().tbl_View.selectionModel().setCurrentIndex(pndx, QItemSelectionModel.Select)
self.parent().tbl_View.edit(pndx)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 19:20

























answered Nov 12 '18 at 20:33









eyllanesceyllanesc

75.9k103156




75.9k103156












  • Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

    – Elcid_91
    Nov 13 '18 at 0:55












  • @Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

    – eyllanesc
    Nov 13 '18 at 1:13











  • Made another edit to exemplify the issue. Thanks eyllanesc.

    – Elcid_91
    Nov 13 '18 at 18:35











  • Thanks eyllanesc. I now understand the modeling. Cheers!

    – Elcid_91
    Nov 16 '18 at 13:49

















  • Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

    – Elcid_91
    Nov 13 '18 at 0:55












  • @Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

    – eyllanesc
    Nov 13 '18 at 1:13











  • Made another edit to exemplify the issue. Thanks eyllanesc.

    – Elcid_91
    Nov 13 '18 at 18:35











  • Thanks eyllanesc. I now understand the modeling. Cheers!

    – Elcid_91
    Nov 16 '18 at 13:49
















Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

– Elcid_91
Nov 13 '18 at 0:55






Eyllanesc, the solution worked; however, if the table is filtered, an edit is returning the wrong record data. Filter one of the rows with a "1" in the last column. Double click any record under the one you filtered and the data is from another row.

– Elcid_91
Nov 13 '18 at 0:55














@Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

– eyllanesc
Nov 13 '18 at 1:13





@Elcid_91 I have created my test data and I see that it works correctly, you could explain yourself better.

– eyllanesc
Nov 13 '18 at 1:13













Made another edit to exemplify the issue. Thanks eyllanesc.

– Elcid_91
Nov 13 '18 at 18:35





Made another edit to exemplify the issue. Thanks eyllanesc.

– Elcid_91
Nov 13 '18 at 18:35













Thanks eyllanesc. I now understand the modeling. Cheers!

– Elcid_91
Nov 16 '18 at 13:49





Thanks eyllanesc. I now understand the modeling. Cheers!

– Elcid_91
Nov 16 '18 at 13:49

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268053%2fissue-with-qtableview-edit-mode%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線