Load QDialog directly from UI-File?
Asked Answered
E

1

6

I work with QT Designer and create my GUIs with it. To launch the main program, I use this code:

import sys
from PyQt4 import uic, QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8

except AttributeError:
    def _fromUtf8(s):
    return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)

except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

def main():
    app = QtGui.QApplication(sys.argv)
    myWindow = MyWindowClass()
    myWindow.show()
    app.exec_()

main_dialog = uic.loadUiType("GUI.ui")[0]

class MyWindowClass(QtGui.QMainWindow, main_dialog):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)

        self.setupUi(self)

if __name__ == "__main__": 
    main()

So with this line main_dialog = uic.loadUiType("GUI.ui")[0] I define my created UI-File.

Now when I work with QDialogs I have only accomplished to run them by first creating them, then converting them to Python code (using PYUIC4), then implementing the code in my main python file and run the QDialog this way:

def NameOfDialog(self):
    dialog = Qdialog()            
    dialog.ui = NameOfDialogClass()
    dialog.ui.setupUi(dialog)
    dialog.exec_()

The obvious problem is that whenever I make any tiny change to the GUI I have to go through the entire process again (converting and putting the code in the main code and watch out to not delete any other lines that I added and need to maintain).

I am sure there is a solution to also refer to the UI File of the QDialog directly, but how? I tried the same method like I do for the main window but without success :(

Thanks!

EDIT:

Here is what I have tried in a minimal example, but it's not working. What am I missing?

#-*- encoding: UTF-8 -*-
import sys
from PyQt4 import uic, QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig,     _encoding)

except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

def main():
    app = QtGui.QApplication(sys.argv)
    myWindow = MyWindowClass()
    myWindow.show()
    app.exec_()

main_dialog = uic.loadUiType("GUI.ui")[0]

TestQDialog = uic.loadUiType("Dialog.ui")[0]

class QDialogClass(object, TestQDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)

class MyWindowClass(QtGui.QMainWindow, main_dialog):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.btn_close.clicked.connect(self.dialog)

    def dialog(self):
        dialog = Qdialog()
        dialog.ui = QDialogClass()
        dialog.ui.setupUi(dialog)
        dialog.exec_()

if __name__ == "__main__":
    main()
Elliot answered 30/11, 2016 at 11:14 Comment(2)
Please post the dialog code you tried and which didn't work. All the other code in your answer is irrelevant.Suet
@Suet I added a sample of the code that I tried, but definately not working :(Elliot
S
9

Your dialog class should be defined in exactly the same way as your main-window class. I obviously can't test it myself, but the script should look like this:

import sys
from PyQt4 import uic, QtGui, QtCore

main_dialog = uic.loadUiType("GUI.ui")[0]    
TestQDialog = uic.loadUiType("Dialog.ui")[0]

class QDialogClass(QtGui.QDialog, TestQDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)

class MyWindowClass(QtGui.QMainWindow, main_dialog):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.btn_close.clicked.connect(self.dialog)

    def dialog(self):
        dialog = QDialogClass()
        dialog.exec_()

def main():
    app = QtGui.QApplication(sys.argv)
    myWindow = MyWindowClass()
    myWindow.show()
    app.exec_()

if __name__ == "__main__":  
    main()
Suet answered 1/12, 2016 at 1:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.