直接从 UI 文件加载 QDialog? [英] Load QDialog directly from UI-File?

查看:59
本文介绍了直接从 UI 文件加载 QDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QT Designer 并用它创建我的 GUI.要启动主程序,我使用以下代码:

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

所以用这一行 main_dialog = uic.loadUiType("GUI.ui")[0] 我定义了我创建的 UI 文件.

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

现在,当我使用 QDialogs 时,我只是通过首先创建它们,然后将它们转换为 Python 代码(使用 PYUIC4),然后在我的主 python 文件中实现代码并以这种方式运行 QDialog 来完成运行它们:

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

明显的问题是,每当我对 GUI 进行任何微小的更改时,我都必须再次完成整个过程(转换代码并将其放入主代码中,并注意不要删除我添加和需要的任何其他行维护).

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

我确定有一个解决方案也可以直接引用 QDialog 的 UI 文件,但是如何?我尝试了与主窗口相同的方法,但没有成功:(

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

谢谢!

这是我在一个最小的例子中尝试过的,但它不起作用.我错过了什么?

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

推荐答案

您的对话框类的定义方式应该与您的主窗口类完全相同.我显然不能自己测试,但脚本应该是这样的:

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

这篇关于直接从 UI 文件加载 QDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆