Simon
Simon

Reputation: 125

Clearing QTableView in PyQt5

I am working with PyQt5 and I am trying to clear a QTreeView by pressing a button. The program is supposed to take a path from the user with a QLineEdit and then display it on the TreeView. So far it's working great. The thing is that I can't figure out how to clear the view once I'm finished or maybe if I typed in the wrong path. I know that I could just use the clear() function but it works only with a Widget, not with a View. If I use the reset() function it just displays the "This PC" folder without completey clearing the Tree. I am going to include the code just in case.

from PyQt5 import QtWidgets as qtw
import sys


class MainWindow(qtw.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Init UI
        self.tree = qtw.QTreeView()
        self.model = qtw.QFileSystemModel()

        # Items
        self.path_input = qtw.QLineEdit()
        path_label = qtw.QLabel("Enter a path to begin: ")
        check_btn = qtw.QPushButton("Check")  # To display the items
        clear_btn = qtw.QPushButton("Clear")  # To clear the TreeView
        start_btn = qtw.QPushButton("Start")  # Not implemented yet

        # Layouts
        top_h_layout = qtw.QHBoxLayout()
        top_h_layout.addWidget(path_label)
        top_h_layout.addWidget(self.path_input)
        top_h_layout.addWidget(check_btn)
        bot_h_layout = qtw.QHBoxLayout()
        bot_h_layout.addWidget(clear_btn)
        bot_h_layout.addWidget(start_btn)
        main_v_layout = qtw.QVBoxLayout()
        main_v_layout.addLayout(top_h_layout)
        main_v_layout.addWidget(self.tree)
        main_v_layout.addLayout(bot_h_layout)
        self.setLayout(main_v_layout)

        check_btn.clicked.connect(self.init_model)
        clear_btn.clicked.connect(self.clear_model)

        self.show()

    def init_model(self):
        self.model.setRootPath(self.path_input.text())
        self.tree.setModel(self.model)
        self.tree.setRootIndex(self.model.index(self.path_input.text()))

    def clear_model(self):
        self.tree.reset()  # This is where I get the problem


if __name__ == "__main__":
    app = qtw.QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

Upvotes: 1

Views: 1624

Answers (1)

musicamante
musicamante

Reputation: 48231

You can't "clear a view", as the view only shows the model's content, it cannot directly modify (or "clear") it. Also, reset() does not do what you're trying to achieve:

void QAbstractItemView::reset()

Reset the internal state of the view.

The state, not the model. As you can see, in your case it only resets the root node in its collapsed state.

What you need to clear is the model, but since you clearly cannot clear a filesystem model, the solution is to set the view's state as it was before starting the search: by removing the model from the view.

    def clear_model(self):
        self.tree.setModel(None)

Upvotes: 2

Related Questions