-1

I tried to separate my program's functionality from the Ui class (generated by PyQt5 Designer), as specified here and here. Problem is that when run logic.py the Ui shows up, but the program's functions do not work at all. I am using PyCharm, so I tried invalidating the caches, installing pyqt5-stubs but that did not work as well. Any help would be highly appreciated!

ui.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("File Manager")
        MainWindow.resize(1120, 800)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.button1 = QtWidgets.QPushButton(self.centralwidget)
        self.button1.setGeometry(QtCore.QRect(10, 80, 121, 41))

        self.listView = QtWidgets.QListView(self.centralwidget)
        self.listView.setGeometry(QtCore.QRect(10, 190, 221, 471))
        self.listView.setObjectName("listView")
        path = r"C:\Test_folder"
        self.dirModel = QtWidgets.QFileSystemModel()
        self.dirModel.setRootPath(QtCore.QDir.rootPath())
        self.listView.setModel(self.dirModel)
        self.listView.setRootIndex(self.dirModel.index(path))


        self.fileModel = QtWidgets.QFileSystemModel()
        self.treeView = QtWidgets.QTreeView(self.centralwidget)
        self.treeView.setModel(self.fileModel)
        self.treeView.setRootIndex(self.fileModel.index(path))
        self.treeView.setGeometry(QtCore.QRect(270, 90, 801, 571))
        self.treeView.setObjectName("treeView")

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("File Manager", "File Manager"))
        self.button1.setText(_translate("MainWindow", "+ New File"))

logic.py

from PyQt5.QtWidgets import *
from ui import Ui_MainWindow
import sys

class Logic(Ui_MainWindow):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()

        # Set up the user interface
        self.setupUi(self)

        # "New Project" button function call
        self.button1.clicked.connect(self.make_document)

        # When folder in listView is clicked, the items stored in it are
        # displayed in the treeview.
        self.listView.clicked.connect(self.on_click)

    # Program Functions
    def on_click(self, index):
        """When a directory is selected in the list view, show all of its files
        in the tree view."""
        path = self.dirModel.fileInfo(index).absoluteFilePath()
        self.treeView.setRootIndex(self.fileModel.setRootPath(path))

    def make_document(self):
        print("Making document")

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

1 Answers1

1

You're making some mistakes.

First of all, you never created an instance of Logic.

Then, in order to use the files generated by pyuic a QWidget has to be created, so that the UI can be built in it with the UI_* object.

You don't need to subclass Ui_MainWindow, but the widget class that you're going to use (QMainWindow, in your case). You can then choose to use the single or multiple inheritance approach, but I suggest you to use the latter.

Single inheritance:

class Logic(QMainWindow):
    def __init__(self):
        super(Logic, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.button1.clicked.connect(self.make_document)
        # ...

Multiple inheritance:

class Logic(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(Logic, self).__init__()

        self.setupUi(self)

        self.button1.clicked.connect(self.make_document)

Obviously, this implies that it's the Logic() instance that has to be created when starting the application, not Ui_MainWindow:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    logic = Logic()
    logic.show()
    sys.exit(app.exec_())

For more insight, carefully read the official documentation about using Designer.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Would you be so kind as to elaborate on your first two points? I implemented the inheritance properly, but it is still not working. Otherwise, I read the documentation (as linked above) but I am still having trouble figuring it out. Thank you. – Dimovik Mar 02 '20 at 15:39
  • Have you created the instance of Logic() in the `if __name__...` statement? Note that you only have to create that instance, there's no need to `ui = Ui_MainWindow()` in there, as the Logic class takes care of that. Read the edit at the bottom of the answer. – musicamante Mar 02 '20 at 15:43
  • Ah yes, I understand now and it works perfectly. Thank you so very much! :) – Dimovik Mar 02 '20 at 15:51