Results 1 to 1 of 1

Thread: (PyQt) User dictated row arrangment?

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

    Default Re: (PyQt) User dictated row arrangment?

    Hello.
    WorkFlow:
    [QAbstractTableModel]<===>[QtableView]

    Vertical Header is moveable, and when the user enacts such functionality, the following is called (from QtableView):

    Qt Code:
    1. self.tableView.verticalHeader().setMovable(True)
    2. self.tableView.verticalHeader().sectionMoved.connect(self.rowsChanged)
    3. ...
    4.  
    5. def rowsChanged(self,dataRow,oldDisplayRow,newDisplayRow):
    6. self.tableData.resortRows(dataRow,oldDisplayRow,newDisplayRow)
    To copy to clipboard, switch view to plain text mode 

    QAbstractTableModel catches the call, and enacts the following:

    Qt Code:
    1. def resortRows(self,dataRow,oldDisplayRow,newDisplayRow):
    2. print "you moved dataRow %s from displayRow %s to displayRow %s" % (dataRow,oldDisplayRow,newDisplayRow)
    3. self.rows.insert(newDisplayRow,self.rows.pop(dataRow))
    4. self.layoutChanged.emit()
    To copy to clipboard, switch view to plain text mode 

    While the underlying data structure (self.rows) is properly rearranged, the QtableView does not reflect these changes - and rather exhibits random offsetting of rows; non-synchronous with the ACTUAL changes made.




    Are the 'beginInsertRows(...)/endInstertRows()' and 'beginRemoveRows(...)/endRemoveRows()' nessecary?
    As when I implement these as follows, neither the QtableView, nor the underlying data (self.rows) is properly altered:
    Qt Code:
    1. def resortRows(self,dataRow,oldDisplayRow,newDisplayRow):
    2. print "you moved dataRow %s from displayRow %s to displayRow %s" % (dataRow,oldDisplayRow,newDisplayRow)
    3. self.beginInsertRows(QtCore.QModelIndex(),newDisplayRow,newDisplayRow)
    4. self.rows.insert(newDisplayRow,self.rows[dataRow])
    5. self.endInsertRows()
    6. self.beginRemoveRows(QtCore.QModelIndex(),dataRow,dataRow)
    7. self.rows.pop(dataRow)
    8. self.endRemoveRows()
    To copy to clipboard, switch view to plain text mode 

    A billion thanks!


    Added after 23 minutes:


    Doh!

    Not yet sure if this is the most elegant manner, but implementing the following in QAbstractModel solved the problems (for now):

    Qt Code:
    1. def resortRows(self,dataRow,oldDisplayRow,newDisplayRow):
    2. print "you moved dataRow %s from displayRow %s to displayRow %s" % (dataRow,oldDisplayRow,newDisplayRow)
    3. cacheData = self.rows[dataRow]
    4. self.removeRows(dataRow)
    5. self.beginInsertRows(QtCore.QModelIndex(),newDisplayRow,newDisplayRow)
    6. self.rows.insert(newDisplayRow,cacheData)
    7. self.endInsertRows()
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. # removes row from model (only if entire row-children are selected)
    2. def removeRows(self, row):
    3. self.beginRemoveRows(QtCore.QModelIndex(),row,row)
    4. self.rows.pop(int(row))
    5. self.endRemoveRows()
    To copy to clipboard, switch view to plain text mode 
    Last edited by jkrienert; 14th January 2015 at 16:32.

Similar Threads

  1. PyQt savestate
    By Dariusz in forum Newbie
    Replies: 0
    Last Post: 2nd October 2013, 10:54
  2. Replies: 6
    Last Post: 3rd December 2012, 08:26
  3. Upgrading from PyQt 4.5 to PyQt 4.7
    By RogerCon in forum Installation and Deployment
    Replies: 0
    Last Post: 14th July 2010, 19:52
  4. PyQt
    By helvin in forum Newbie
    Replies: 1
    Last Post: 15th April 2009, 09:56
  5. Replies: 2
    Last Post: 27th November 2008, 11:16

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.