nephilimrising nephilimrising - 2 months ago 7
Python Question

Calling class that initiates UI class in python

I have an issue while creating my small Python project. I am used to Java and this is still quite new to me.
The problem is i create a UI class from QtCreator. Then convert it to

.py
and import to my project. I have a class that for now is considered
main
that initiates and runs the UI class. The problem is i need to have a class that is really the
main
class and when app is started this class to call the second class that initiates the UI.
To sum it up now when app is started i have
A > calls > B(UI)
.
I need it to be
A > calls > B > init > C(UI)
.
Is that possible? My idea is that B must be only a manager class that sets up the UI and run it.
Here is my working code so far:
1.Manager class-

class mainpanelManager(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = mainpanelManager()
window.show()
sys.exit(app.exec_())


and UI class is typical UI generated class. I will post only constructor:

class Ui_MainWindow(QtGui.QWindow):
def setupUi(self, MainWindow):


In this case
mainpaneManager
runs
Ui_MainWindow
after element initiation.
I want to have a 3rd class that calls
mainpanelManager
.
What do i do with this
__name__
function? Move it?
Thanks in advance!

Answer

I'm not exactly sure I understand what you're asking for, but it would seem to be as simple as this:

class Main(object):
    def __init__(self):
        self.window = MainPanelManager()
        self.window.show()             

class MainPanelManager(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.settingsButton.clicked.connect(self.editSettings)
        self.loadSettings()

    def editSettings(self):
        dialog = SettingsDialog(self)
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            self.loadSettings()

    def loadSettings(self):
        # do stuff to load settings...

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    main = Main()
    sys.exit(app.exec_())