PyQt与自定义插槽工作,Qt设计师没有

问题描述 投票:2回答:2

我正在尝试围绕我已经拥有的一些代码构建GUI。我知道如何在手动构建GUI时执行此操作,但在将此添加到Qt Designer和pyuic生成的python代码时会遇到困难。作为一个例子,我可能需要一个允许用户指向文件的按钮,我手动操作,这样做有效:

import sys
from PyQt4 import QtGui


class Example(QtGui.QWidget):    
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):        

        btn = QtGui.QPushButton('Open File', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)     
        btn.clicked.connect(self.loadFile)

        self.setGeometry(300, 300, 250, 150)
        self.show()

    def loadFile(self):
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        # some custom code for reading file and storing it

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

但是,当我尝试在Qt Designer代码中执行相同操作时,程序在到达文件对话框之前停止。

from PyQt4 import QtCore, QtGui

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)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(130, 100, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.loadFile)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "Open File", None))

    def loadFile(self):
        print('loadFile1')
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        print('loadFile2')


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

这仅打印loadFile()中的第一个语句,但不会打开文件对话框窗口。我究竟做错了什么?

python pyqt pyqt4 qfiledialog pyuic
2个回答
1
投票

根据documentation

QString getOpenFileName(QWidget parent = None,QString caption ='',QString directory ='',QString filter ='',Options options = 0)

QString getOpenFileName(QWidget parent = None,QString caption ='',QString directory ='',QString filter ='',QString selectedFilter ='',Options options = 0)

您需要将父窗口小部件传递给“无”或“无”,在您的情况下,self不属于对象类型。

你必须改变

 QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')

QtGui.QFileDialog.getOpenFileName(None, 'Open file', '/home')

0
投票

只要我能避免,我真的不喜欢使用pyuic。您可以尝试一种更简单的方法,它可以减少您的代码。假设您的UI文件名为something.ui,并且您已在QT Designer的某个按钮中命名了您的按钮,那么代码将为:

from PyQt4 import QtCore, QtGui, uic
Ui_somewindow, _ = uic.loadUiType("something.ui") #the path to your UI

class SomeWindow(QtGui.QMainWindow, Ui_somewindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_somewindow.__init__(self)
        self.setupUi(self)
        self.somebutton.clicked.connect(self.loadFile)

   def loadFile(self):
        print('loadFile1')
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        print('loadFile2')

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

请注意,如果你有QDialog用QDialog替换QMainWindow。希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.