Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Possible conversion from QStandardItemModel to QAbstractTableModel?

  1. #21
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Possible conversion from QStandardItemModel to QAbstractTableModel?

    It is odd, that printing 'self.rows' at the end of the setData fuction returns a list with proper updates, yet if printing 'self.rows' at the beginning of the saveIt fuction, the list does not show any of these updates (please see attached image at footer)
    It seems maybe anda_skoa is right; in that possibly rather than reference - the current is creating a duplicate array...

    Below are changes reflected to ensure that (previously line 60) is getting a reference and not a copy [hopefully its correct]:
    Qt Code:
    1. def setData(self, index, value, role):
    2. if index.isValid() and role == Qt.EditRole:
    3. ## col = index.column()
    4. ## row = self.rows[index.row()]
    5. ## row[col]=value
    6. self.rows[index.row()][index.column()]=value
    7. print self.rows[0]
    8. self.dataChanged.emit(index, index)
    9. return True
    10. return False
    11. def flags(self, index):
    12. return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
    13. def saveIt(self):
    14. print self.rows[0]
    15. with open(self.fileName, "wb") as fileOutput:
    16. writer = csv.writer(fileOutput)
    17. writer.writerow(self.header[0])
    18. for rowNumber in range(len(self.rows)):
    19. fields = self.rows[rowNumber]
    20. writer.writerow(fields)
    21. print ':)'
    To copy to clipboard, switch view to plain text mode 
    misMatching.jpg
    Last edited by jkrienert; 27th December 2014 at 21:15.

  2. #22
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Possible conversion from QStandardItemModel to QAbstractTableModel?

    Suspicious of calling saveIt() in the AbstractModel via the QtableView nested button. I wonder if its causing the initial load-up of the CSV data to overwrite any editing just before save?
    If this is the case, how can such be avoided?

    Button call function in QtableView:
    Qt Code:
    1. @QtCore.pyqtSlot()
    2. def on_pushButtonSave_clicked(self):
    3. CSVModel(self).saveIt()
    To copy to clipboard, switch view to plain text mode 

    __init__ for AbstractTableModel, and subsequent saveIt() function therein:
    Qt Code:
    1. def __init__(self, fileName, parent=None):
    2. super(CSVModel,self).__init__()
    3. self.header = []
    4. self.rows = []
    5. self.fileName = r'E:\Documents\SIUC\2014\Fall\440 - Hydro\QGIS\test_bay\CSVtesting\mfLayer1_Grid.csv'
    6. with open(self.fileName, "rb") as fileInput:
    7. for idx, row in enumerate(csv.reader(fileInput)):
    8. headerIDx = 0
    9. if idx is headerIDx:
    10. self.header.append(row)
    11. elif idx>headerIDx:
    12. items = [field for field in row]
    13. self.rows.append(items)
    14. self.rowsLoaded = CSVModel.activeRows
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. def saveIt(self):
    2. with open(self.fileName, "wb") as fileOutput:
    3. writer = csv.writer(fileOutput)
    4. writer.writerow(self.header[0])
    5. for rowNumber in range(len(self.rows)):
    6. fields = self.rows[rowNumber]
    7. writer.writerow(fields)
    To copy to clipboard, switch view to plain text mode 

  3. #23
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Possible conversion from QStandardItemModel to QAbstractTableModel?

    Could it be that you are creating a new model and call saveIt() on that instance instead of calling saveIt() in the model used by the view?

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    jkrienert (28th December 2014)

  5. #24
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default [solved]

    [SOLVED]

    It could be, and would be indeed!
    A glaring mistake I overlooked. Thank you anda_skoa.
    This postings conclusion successfully achieved the migration from standard to abstracttablemodel.
    Now on to refining flags and connections in this module!

    For reference, the following resolved the problem:

    Qt Code:
    1. self.tableData = CSVModel(self)
    2.  
    3. ...
    4.  
    5. @QtCore.pyqtSlot()
    6. def on_pushButtonSave_clicked(self):
    7. self.tableData.saveIt()
    To copy to clipboard, switch view to plain text mode 
    Last edited by jkrienert; 28th December 2014 at 13:36. Reason: 'solved' header added

Similar Threads

  1. Update QAbstractTableModel?
    By adutzu89 in forum Newbie
    Replies: 2
    Last Post: 4th June 2014, 22:17
  2. TableView and QAbstractTableModel
    By MattieB in forum Newbie
    Replies: 1
    Last Post: 11th December 2013, 21:57
  3. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 10:30
  4. Using QAbstractTableModel.parent
    By Max Yaffe in forum Newbie
    Replies: 7
    Last Post: 15th June 2007, 16:21
  5. QAbstractTableModel for QTreeView?
    By Michiel in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2007, 10:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.