0

When I right click a file in tree view, context menu and it's underlying methods work.

However, when I right click on empty space and context menu opens, delete and archive methods apply on my working folder itself (i.e. where the test_gui.py file is stored).

I'm fine with the context menu popping up on empty space, I just don't want it to be able to do anything in that case.

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os
import send2trash
import shutil

class Ui_MainWindow(QtWidgets.QWidget):
    def setupUi(self, MainWindow):
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)

        # Treeview model, path should contain files for test purpose
        path = r"C:\Test_gui"
        self.fileModel = QtWidgets.QFileSystemModel()
        self.fileModel.setReadOnly(False)

        # Treeview functionality
        self.treeView = QtWidgets.QTreeView(self.centralwidget)
        self.treeView.setModel(self.fileModel)
        self.treeView.setRootIndex(self.fileModel.setRootPath(path))
        self.treeView.setGeometry(QtCore.QRect(190, 130, 541, 381))
        self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.treeView.customContextMenuRequested.connect(self.openMenu)

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate


    def delete(self, index):
        """ Send the selected file to the recycle bin"""
        path = self.fileModel.fileInfo(index).absoluteFilePath()
        # When context menu is opened (right click) on a file, send2trash 
        # sends file to the bin. However, if context menu is open on empty 
        # space, delete method sends my working directory to the bin. 
        print(os.path.abspath(path))
        # send2trash.send2trash(os.path.abspath(path))


    def archive(self, index):
        """Move selected file to archive"""
        source = self.fileModel.fileInfo(index).absoluteFilePath()
        destination = r"C:\Test_gui_archive"
        # Same as above, but the archive method sends the working directory 
        # to archive.
        print(os.path.abspath(source))
        # shutil.move(os.path.abspath(source), os.path.abspath(destination))


    def openMenu(self, position):
        """Setup a context menu to open upon right click."""
        index = self.treeView.indexAt(position)
        menu = QtWidgets.QMenu(self)
        delete_action = menu.addAction("Send to trashbin")
        archive_action = menu.addAction("Move to archive")
        action = menu.exec_(self.treeView.viewport().mapToGlobal(position))
        if action == delete_action:
            self.delete(index)
        if action == archive_action:
            self.archive(index)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Tried to apply the logic from this thread, but was not successful. Advice would be highly appreciated!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

1

To verify that you are in the empty space you can use the index as it will be invalid.

According to this you can make several decisions:

  • Disable the QActions:

    def openMenu(self, position):
        """Setup a context menu to open upon right click."""
        index = self.treeView.indexAt(position)
        menu = QtWidgets.QMenu(self)
        delete_action = menu.addAction("Send to trashbin")
        archive_action = menu.addAction("Move to archive")
        for action in (delete_action, archive_action):
            action.setEnabled(index.isValid())
        action = menu.exec_(self.treeView.viewport().mapToGlobal(position))
        if action == delete_action:
            self.delete(index)
        if action == archive_action:
            self.archive(index)
  • Or not show the QMenu:

    def openMenu(self, position):
        """Setup a context menu to open upon right click."""
        index = self.treeView.indexAt(position)
        if not index.isValid():
            return
        menu = QtWidgets.QMenu(self)
        delete_action = menu.addAction("Send to trashbin")
        archive_action = menu.addAction("Move to archive")
        action = menu.exec_(self.treeView.viewport().mapToGlobal(position))
        if action == delete_action:
            self.delete(index)
        if action == archive_action:
            self.archive(index)

Note: I recommend you not to modify the code generated by Qt Designer but to implement a new class that inherits from the appropriate widget and use the initial class to fill it. For more information read this and Using the Generated Code.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241