Results 1 to 3 of 3

Thread: disabled QTableView cell deselected after soting

  1. #1
    Join Date
    Mar 2010
    Location
    Berlin
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default disabled QTableView cell deselected after soting

    Hello,

    I have found a strange behavior with a combo of QTableView, QSortFilterProxyView and disabled items. If you select a row in the table from the example below then sort by a column, the cell in the last column will be deselected after sorting. This happens only for cells with ItemIsSelectable as it's only item flag. There is nothong fancy or special in the example below, only standard components. Is there something I can do about this?

    Example:

    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    3. import sys
    4.  
    5. my_array = [['00', '01', '02'],
    6. ['10', '11', '12'],
    7. ['20', '21', '22']]
    8.  
    9. def main():
    10. app = QApplication(sys.argv)
    11. w = MyWindow()
    12. w.show()
    13. sys.exit(app.exec_())
    14.  
    15.  
    16. class MyWindow(QWidget):
    17. def __init__(self, *args):
    18. QWidget.__init__(self, *args)
    19.  
    20. sorttablemodel = QSortFilterProxyModel(self)
    21. tablemodel = MyTableModel(my_array, self)
    22. sorttablemodel.setSourceModel(tablemodel)
    23. tableview = QTableView()
    24. tableview.setModel(sorttablemodel)
    25. tableview.setSelectionBehavior(QAbstractItemView.SelectRows)
    26. tableview.setSortingEnabled(True)
    27. layout = QVBoxLayout(self)
    28. layout.addWidget(tableview)
    29. self.setLayout(layout)
    30.  
    31.  
    32. class MyTableModel(QAbstractTableModel):
    33. def __init__(self, datain, parent=None, *args):
    34. QAbstractTableModel.__init__(self, parent, *args)
    35. self.arraydata = datain
    36.  
    37. def rowCount(self, parent):
    38. return len(self.arraydata)
    39.  
    40. def columnCount(self, parent):
    41. return len(self.arraydata[0])
    42.  
    43. def data(self, index, role):
    44. if not index.isValid():
    45. return QVariant()
    46. elif role != Qt.DisplayRole:
    47. return QVariant()
    48. return QVariant(self.arraydata[index.row()][index.column()])
    49.  
    50. def flags(self, index):
    51. if index.column() == 2:
    52. return Qt.ItemIsSelectable
    53. return QAbstractTableModel.flags(self, index)
    54.  
    55. if __name__ == "__main__":
    56. main()
    To copy to clipboard, switch view to plain text mode 

    Cheers

    Sebastian

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: disabled QTableView cell deselected after soting

    I am not a Python coder so I am not sure - but it looks like your are allowing the item to be selectable only if its in column 2.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2010
    Location
    Berlin
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: disabled QTableView cell deselected after soting

    For column 2 I specify the item to be ONLY selectable, but not enabled. Other columns get the default flags from the base class implmentation: http://qt-project.org/doc/qt-4.8/qab...del.html#flags
    Thanks for having a look!

Similar Threads

  1. Jump to a cell in QTableView
    By toodles in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2012, 06:07
  2. QGraphicsItem deselected on contextMenuEvent
    By D Cell in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2010, 05:25
  3. Add QPushButton to cell in QTableView?
    By nikhil in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 09:01
  4. Enabling scroll bars in disabled QTableView
    By Koas in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2009, 10:39
  5. How to tell if cell in QTableView is visible ??
    By steviekm3 in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2009, 17:57

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
  •  
Qt is a trademark of The Qt Company.