Load QDialog directly from UI-File?

↘锁芯ラ 提交于 2019-12-22 08:06:49

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/40886912/load-qdialog-directly-from-ui-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!