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_())