1
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        hlay = QHBoxLayout(self)

        self.listview = QListView()
        self.listview2 = QListView()

        hlay.addWidget(self.listview)
        hlay.addWidget(self.listview2)

        path = r'C:\Users\Desktop\Project'

        self.fileModel = QFileSystemModel()
        self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)

        self.listview.setRootIndex(self.fileModel.index(path))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

I want to display the files in my listview from my folder with path described in the code and able to select them, the files I selected will be displayed in my listview2, However, the listview doesn't show the files in this path. Can anyone help me with it?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Cindy
  • 425
  • 1
  • 3
  • 10
  • Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown? – eyllanesc Nov 12 '18 at 21:51
  • Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted? – eyllanesc Nov 13 '18 at 20:07
  • @eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView – Cindy Nov 13 '18 at 20:10
  • Now it's clearer, what information other than the name of the file do you want the second QListView to have? – eyllanesc Nov 13 '18 at 20:12
  • You say: *By now I just want the files*, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView? – eyllanesc Nov 13 '18 at 20:20
  • just the filenames, I am trying to run this files I selected in python to see if they have exit code 0 or 1, so perhaps I will put a pushbutton at the bottom and when i click the button, python will run this files to check if they run successfully or not. – Cindy Nov 13 '18 at 20:23
  • okay, now what color does it depend on?, that is, if I had n-files as they would be the colors of them – eyllanesc Nov 13 '18 at 20:26

1 Answers1

1

The files are not displayed because you have not set a rootPath in the QFileSystemModel.

On the other hand the second QListView must have a model where items are added or removed as they are selected or deselected, for this you must use the selectionChanged signal of selectionModel() of the first QListView, that signal transports the information of the selected and deselected items.

To change the color you must obtain the QStandardItem and use the setData() method with the Qt::BackgroundRole role. In the example in each second the color is changed randomly

import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super(Widget, self).__init__(*args, **kwargs)
        self.listview = QtWidgets.QListView()
        self.listview2 = QtWidgets.QListView()

        path = r'C:\Users\Desktop\Project'

        self.fileModel = QtWidgets.QFileSystemModel(self)
        self.fileModel.setRootPath(path)
        self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.listview.setModel(self.fileModel)
        self.listview.setRootIndex(self.fileModel.index(path))
        self.listview.selectionModel().selectionChanged.connect(self.on_selectionChanged)
        self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

        self.model = QtGui.QStandardItemModel(self)
        self.listview2.setModel(self.model)

        hlay = QtWidgets.QHBoxLayout(self)
        hlay.addWidget(self.listview)
        hlay.addWidget(self.listview2)

        timer = QtCore.QTimer(self, interval=1000, timeout=self.test_color)
        timer.start()

    def on_selectionChanged(self, selected, deselected):
        roles = (QtCore.Qt.DisplayRole, 
                 QtWidgets.QFileSystemModel.FilePathRole,
                 QtWidgets.QFileSystemModel.FileNameRole,
                 QtCore.Qt.DecorationRole)

        for ix in selected.indexes():
            it = QtGui.QStandardItem(ix.data())
            for role in roles:
                it.setData(ix.data(role), role)
            it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)
            self.model.appendRow(it)

        filter_role = QtWidgets.QFileSystemModel.FilePathRole
        for ix in deselected.indexes():
            for index in self.model.match(ix.parent(), filter_role, ix.data(filter_role), -1, QtCore.Qt.MatchExactly):
                self.model.removeRow(index.row())

    def test_color(self):
        if self.model.rowCount() > 0:
            n_e = random.randint(0, self.model.rowCount())
            rows_red = random.sample(range(self.model.rowCount()), n_e)
            for row in range(self.model.rowCount()):
                it = self.model.item(row)
                if row in rows_red:
                    it.setData(QtGui.QColor("red"), QtCore.Qt.BackgroundRole)
                else:
                    it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you so much! just one quick question, the `ix in selected.indexes()` are `PyQt5.QtCore.QModelIndex object`, how do I retreive the file name of the ix – Cindy Nov 13 '18 at 21:32
  • @Cindy use `ix.data()` or `ix.data(QtWidgets.QFileSystemModel.FilePathRole)` or `ix.data(QtWidgets.QFileSystemModel.FileNameRole)` – eyllanesc Nov 13 '18 at 21:35