Results 1 to 2 of 2

Thread: How do I set selection in QColumnView?

  1. #1
    Join Date
    May 2010
    Venice, CA
    Qt products
    MacOS X Unix/X11 Windows

    Question How do I set selection in QColumnView?

    Hey all!

    I'm working on a custom asset browser using a QColumnView and I'm having trouble figuring out how to set the selection programatically. Does anyone have any tips/tricks?

    The closest I've come was this:
    myColumnView.selectionModel().select(index, Qt.QItemSelectionModel.SelectCurrent)
    But this has two problems:
    1: It only works if the index I'm trying to select is in the deepest active column.
    2: Also it only highlights the item, it does not trigger the next column to populate based on the selection.

    I'm using a custom ItemModel for my actual project, but I've tested just using QDirModel and gotten the same results.

    If it helps, here's the basic gui I slapped together to try things out on. The copy button stores the index of the current selection and the select button tries to select that index. (Sorry for the python. Feel free to respond with C, if you have something that might help.)

    Qt Code:
    1. import sys
    2. from PyQt4 import Qt, QtCore, QtGui
    4. class Ui_MainWindow(object):
    5. def setupUi(self, MainWindow):
    6. MainWindow.setObjectName("MainWindow")
    7. MainWindow.resize(600, 400)
    8. self.centralwidget = QtGui.QWidget(MainWindow)
    9. self.vertialLayout = QtGui.QVBoxLayout(self.centralwidget)
    10. self.columnView = QtGui.QColumnView(self.centralwidget)
    11. self.model = QtGui.QDirModel()
    12. self.columnView.setModel(self.model)
    13. self.vertialLayout.addWidget(self.columnView)
    14. MainWindow.setCentralWidget(self.centralwidget)
    16. self.savedIndexes = None
    18. self.buttonWidget = QtGui.QWidget(self.centralwidget)
    19. self.buttonLayout = QtGui.QHBoxLayout(self.buttonWidget)
    20. self.buttonLayout.setSpacing(0)
    21. self.buttonLayout.setMargin(0)
    22. self.copyIndexButton = QtGui.QToolButton(self.buttonWidget)
    23. self.copyIndexButton.setText('Copy Index')
    24. self.buttonLayout.addWidget(self.copyIndexButton)
    26. self.selectIndexButton = QtGui.QToolButton(self.buttonWidget)
    27. self.selectIndexButton.setText('Select Index')
    28. self.buttonLayout.addWidget(self.selectIndexButton)
    30. self.spacer = QtGui.QSpacerItem(50, 5, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
    31. self.buttonLayout.addItem(self.spacer)
    32. self.vertialLayout.addWidget(self.buttonWidget)
    34. MainWindow.connect(self.copyIndexButton, Qt.SIGNAL('released()'), self.copyIndex)
    35. MainWindow.connect(self.selectIndexButton, Qt.SIGNAL('released()'), self.selectIndex)
    38. def copyIndex(self):
    39. self.savedIndexes = self.columnView.selectionModel().selection().indexes()
    41. def selectIndex(self):
    42. self.columnView.selectionModel().select(self.savedIndexes[0], Qt.QItemSelectionModel.SelectCurrent)
    44. if __name__ == "__main__":
    45. app = Qt.QApplication(sys.argv)
    46. MainWindow = QtGui.QMainWindow()
    47. ui = Ui_MainWindow()
    48. ui.setupUi(MainWindow)
    50. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 


  2. #2
    Join Date
    Feb 2017
    Qt products
    MacOS X Unix/X11 Windows

    Default Re: How do I set selection in QColumnView?

    @hazzmax I'm also trying to do the same. It selects the row but the next column isn't getting loaded. Have you resolved this?

Similar Threads

  1. How to turn off this annoying feature of QColumnView?
    By jwieland in forum Qt Programming
    Replies: 11
    Last Post: 3rd September 2010, 18:38
  2. custom QColumnView, some questions
    By Mystical Groovy in forum Qt Programming
    Replies: 1
    Last Post: 9th October 2009, 15:03
  3. selection
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2009, 10:37
  4. QModelIndex.column() error in QColumnView?
    By tlustoch in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:45
  5. QDirModel and QColumnView question
    By yartov in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 22:56

Tags for this Thread


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.