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
  3.  
  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)
  15.  
  16. self.savedIndexes = None
  17.  
  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)
  25.  
  26. self.selectIndexButton = QtGui.QToolButton(self.buttonWidget)
  27. self.selectIndexButton.setText('Select Index')
  28. self.buttonLayout.addWidget(self.selectIndexButton)
  29.  
  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)
  33.  
  34. MainWindow.connect(self.copyIndexButton, Qt.SIGNAL('released()'), self.copyIndex)
  35. MainWindow.connect(self.selectIndexButton, Qt.SIGNAL('released()'), self.selectIndex)
  36.  
  37.  
  38. def copyIndex(self):
  39. self.savedIndexes = self.columnView.selectionModel().selection().indexes()
  40.  
  41. def selectIndex(self):
  42. self.columnView.selectionModel().select(self.savedIndexes[0], Qt.QItemSelectionModel.SelectCurrent)
  43.  
  44. if __name__ == "__main__":
  45. app = Qt.QApplication(sys.argv)
  46. MainWindow = QtGui.QMainWindow()
  47. ui = Ui_MainWindow()
  48. ui.setupUi(MainWindow)
  49. MainWindow.show()
  50. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode 

Thanks!