Pyqt4 QFileDialog构造函数:什么用作父参数?

时间:2016-05-01 14:29:21

标签: python python-2.7 pyqt pyqt4

我正在使用pyqt4和python 2.7.11。我的文件/目录对话框有问题:我不知道要用什么作为父参数。

如果我使用“self”或“None”,PyCharm告诉我它正在期待“QFileDialog”,它分别得到“AppTest”和“None”。

如果我使用“parent = self”或“parent = None”,PyCharm会告诉我“self”参数未填充。

请参阅下面的示例代码;我正在使用Qt Designer创建ui,如果您需要该代码,请告诉我。

import sys
from PyQt4 import QtGui
import ui_test as main_frame


class AppTest(QtGui.QMainWindow, main_frame.Ui_MainAppWindow):
    def __init__(self, parent=None):
        super(AppTest, self).__init__(parent)
        self.setupUi(self)
        self.pushButton1.clicked.connect(self.dialog1)
        self.show()

    def dialog1(self):
        file1 = str(QtGui.QFileDialog.getOpenFileName(
                parent=self,
                caption="Locate file 1",
                directory="",
                filter="Graphics files (*.jpg *.jpeg)"))

        print file1


def main():
    app = QtGui.QApplication(sys.argv)
    gui = AppTest()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

编辑:这是ui的代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test_ui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

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_MainAppWindow(object):
    def setupUi(self, MainAppWindow):
        MainAppWindow.setObjectName(_fromUtf8("MainAppWindow"))
        MainAppWindow.resize(269, 195)
        self.centralwidget = QtGui.QWidget(MainAppWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.pushButton1 = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton1.sizePolicy().hasHeightForWidth())
        self.pushButton1.setSizePolicy(sizePolicy)
        self.pushButton1.setObjectName(_fromUtf8("pushButton1"))
        self.verticalLayout.addWidget(self.pushButton1)
        self.pushButton2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton2.setObjectName(_fromUtf8("pushButton2"))
        self.verticalLayout.addWidget(self.pushButton2)
        MainAppWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainAppWindow)
        QtCore.QMetaObject.connectSlotsByName(MainAppWindow)

    def retranslateUi(self, MainAppWindow):
        MainAppWindow.setWindowTitle(_translate("MainAppWindow", "MainWindow", None))
        self.pushButton1.setText(_translate("MainAppWindow", "PushButton1", None))
        self.pushButton2.setText(_translate("MainAppWindow", "PushButton2", None))

0 个答案:

没有答案