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.)
Code:
import sys
from PyQt4 import Qt, QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 400)
self.
centralwidget = QtGui.
QWidget(MainWindow
) self.
vertialLayout = QtGui.
QVBoxLayout(self.
centralwidget) self.columnView = QtGui.QColumnView(self.centralwidget)
self.columnView.setModel(self.model)
self.vertialLayout.addWidget(self.columnView)
MainWindow.setCentralWidget(self.centralwidget)
self.savedIndexes = None
self.
buttonWidget = QtGui.
QWidget(self.
centralwidget) self.
buttonLayout = QtGui.
QHBoxLayout(self.
buttonWidget) self.buttonLayout.setSpacing(0)
self.buttonLayout.setMargin(0)
self.
copyIndexButton = QtGui.
QToolButton(self.
buttonWidget) self.copyIndexButton.setText('Copy Index')
self.buttonLayout.addWidget(self.copyIndexButton)
self.
selectIndexButton = QtGui.
QToolButton(self.
buttonWidget) self.selectIndexButton.setText('Select Index')
self.buttonLayout.addWidget(self.selectIndexButton)
self.buttonLayout.addItem(self.spacer)
self.vertialLayout.addWidget(self.buttonWidget)
MainWindow.connect(self.copyIndexButton, Qt.SIGNAL('released()'), self.copyIndex)
MainWindow.connect(self.selectIndexButton, Qt.SIGNAL('released()'), self.selectIndex)
def copyIndex(self):
self.savedIndexes = self.columnView.selectionModel().selection().indexes()
def selectIndex(self):
self.
columnView.
selectionModel().
select(self.
savedIndexes[0], Qt.
QItemSelectionModel.
SelectCurrent)
if __name__ == "__main__":
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Thanks!
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?