【问题标题】:PyQt with custom slots works, Qt designer does not带有自定义插槽的 PyQt 可以工作,Qt 设计器不能
【发布时间】:2017-05-26 01:21:18
【问题描述】:

我正在尝试围绕我已经拥有的一些代码构建一个 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() 中的第一条语句,但不会打开文件对话框窗口。我做错了什么?

【问题讨论】:

  • (1) 阅读生成文件顶部的注释:# WARNING! All changes made in this file will be lost!。 (2) 阅读 PyQt 文档:Using Qt Designer。 (3) 在可以显示 Python 回溯的环境中运行代码。

标签: python pyqt pyqt4 qfiledialog pyuic


【解决方案1】:

根据documentation

QString getOpenFileName (QWidget parent = None, QString caption = '', QString 目录 = '',QString 过滤器 = '',选项选项 = 0)

QString getOpenFileName (QWidget parent = None, QString caption = '', QString 目录 = '', QString 过滤器 = '', QString selectedFilter = '', 选项选项 = 0)

您需要将小部件或 None 作为父级传递,在您的情况下,self 不是对象类型。

你必须改变

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

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

【讨论】:

    【解决方案2】:

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

    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,请将 QMainWindow 替换为 QDialog。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多