Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created March 8, 2017 14:14
Show Gist options
  • Save BigRoy/6eb92c6b0a035c2cfdc492964e6dac7a to your computer and use it in GitHub Desktop.
Save BigRoy/6eb92c6b0a035c2cfdc492964e6dac7a to your computer and use it in GitHub Desktop.
Set QColumnView selection (and update the columns)
import sys
import os
from Qt import QtWidgets, QtCore, QtGui
def main():
app = QtWidgets.QApplication(sys.argv)
path = r"C:\test\subfolder\file.txt"
root_path = os.path.dirname(os.path.dirname(path))
model = QtWidgets.QFileSystemModel()
view = QtWidgets.QColumnView()
view.setModel(model)
# Set root
model.setRootPath(root_path)
root_index = model.index(root_path)
view.setRootIndex(root_index)
# Problem #1:
# Setting the current index does set up the horizontal scrollbar (as if it works)
# but it doesn't actually select anything nor do the columns show up on the right side
index = model.index(path)
assert index.isValid()
view.setCurrentIndex(index)
# Problem #2:
# Setting the selection doesn't seem to do anything either.
selection = view.selectionModel()
selection.select(index, QtGui.QItemSelectionModel.Rows)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
@mottosso
Copy link

mottosso commented Mar 8, 2017

Ok, try this.

import sys
import os
from Qt import QtWidgets, QtCore, QtGui


def main():

    app = QtWidgets.QApplication(sys.argv)

    path = r"C:\test\subfolder\file.txt"
    root_path = os.path.dirname(os.path.dirname(path))

    model = QtWidgets.QFileSystemModel()
    view = QtWidgets.QColumnView()
    view.setModel(model)

    # Set root
    model.setRootPath(root_path)
    root_index = model.index(root_path)
    view.setRootIndex(root_index)

    view.show()

    # Select *after* showing
    index = model.index(0, 0)  # First row, first column
    selection = view.selectionModel()

    print("Selecting %s" % index.data(QtCore.Qt.DisplayRole))
    selection.select(index, QtCore.QItemSelectionModel.Select)

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

@BigRoy
Copy link
Author

BigRoy commented Mar 8, 2017

Still not working as expected.

import sys
from Qt import QtWidgets, QtCore, QtGui


def main():

    app = QtWidgets.QApplication(sys.argv)

    # We set the root to C: and try to select the folder C:/test/subfolder in the columns
    # This will require a folder 'test' that contains a folder 'subfolder' on your C: drive.
    root_path = "C:/"
    path = "C:/test/subfolder"

    model = QtWidgets.QFileSystemModel()
    view = QtWidgets.QColumnView()
    view.setModel(model)

    # Set root
    model.setRootPath(root_path)
    root_index = model.index(root_path)
    view.setRootIndex(root_index)

    view.show()

    index = model.index(path)
    selection = view.selectionModel()

    print("Selecting %s" % index.data(QtWidgets.QFileSystemModel.FilePathRole))
    selection.select(index, QtCore.QItemSelectionModel.Select)

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

@mottosso
Copy link

mottosso commented Mar 8, 2017

This works for me as well.

import sys
from Qt import QtWidgets, QtCore


def main():

    app = QtWidgets.QApplication(sys.argv)

    model = QtWidgets.QFileSystemModel()
    view = QtWidgets.QColumnView()
    view.setModel(model)
    view.show()

    model.setRootPath(None)

    # Select *after* showing
    index = model.index("d:/")
    selection = view.selectionModel()
    print("Selecting '%s'" % index.data(QtCore.Qt.DisplayRole))
    selection.select(index, QtCore.QItemSelectionModel.Select)

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

@BigRoy
Copy link
Author

BigRoy commented Mar 8, 2017

This works for me as well.

That one does work, yes. Are you able to get it to work when "highlighting" something in columns nested down? Basically expanding the columns op to that selection. Even with combinations of scroll to and alike I'm unable to get that to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment