0

I made an interface using Qt designer and saved it in a file main.ui

So, i've tried 2 ways to show my window using python and both returned an empty window: enter image description here

First attempt (using the main.ui directly):

from PySide.QtGui import *
from PySide.QtCore import *
from PySide import QtUiTools

class MainApp(QMainWindow):
    def init(self, *args):
        apply(QMainWindow.__init__,(self,) + args)

        loader = QtUiTools.QUiLoader()
        file = QFile("main.ui")
        file.open(QFile.ReadOnly)
        self.myWidget = loader.load(file, self)
        file.close()

        self.setCentralWidget(self.myWidget)

if __name__ == '__main__':
    import sys
    import os
    print "Running in %s.\n" % os.getcwd()

    app = QApplication(sys.argv)
    window = MainApp()
    window.show()

    app.connect(app, SIGNAL("lastWindowClosed()"),
               app, SLOT("quit()")
    )
    app.exec_()

For the second attempt, I used Pyside-uic.exe to generate a main.py file:

from PySide.QtGui import *
from PySide.QtCore import *
from qt_gui.main import *
import sys

class MainApp(QtGui.QMainWindow, Ui_MainWindow):
    def init(self, parent = None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainApp()
    window.show()
    sys.exit(app.exec_())

I've saw a lot of examples doing the same but none worked for me.

4

2 回答 2

1

你有没有尝试过这样的事情?

from PyQt4 import QtCore
from PyQt4 import QtGui

from PyQt4.uic import loadUiType
# This method of the PyQt4.uic module allows for dynamically loading user
# interfaces created by QtDesigner. See the PyQt4 Reference Guide for more
# info.
Ui_Main = \
    loadUiType(os.path.join(os.path.dirname(__file__),'main.ui'))[0]
class MainApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, info):
        """Setup the Properties dialog."""

        super(MainApp, self).__init__(parent)
        self.setupUi(self)

这个对我有用。只需替换为您在文件Ui_Main中使用的名称即可。.ui

我在PyQt这里使用,但我想它也可以使用PySide

于 2013-11-13T18:41:25.343 回答
0

使用 Pyside-uic 的第二种方法应该可以工作,但您需要__init__正确调用该方法(示例代码中缺少下划线):

class MainApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)
于 2013-11-13T20:03:10.960 回答