Pyqt中的Qfiledialog

时间:2014-08-24 11:29:31

标签: python pyqt

我是PyQt编程的新手,我编写了一个代码来浏览文件。代码如下:

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_mainDialog(object):
    def setupUi(self, mainDialog):
        mainDialog.setObjectName(_fromUtf8("mainDialog"))
        mainDialog.resize(648, 48)
        self.formLayout = QtGui.QFormLayout(mainDialog)
        self.formLayout.setObjectName(_fromUtf8("formLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setContentsMargins(-1, 2, -1, -1)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.label = QtGui.QLabel(mainDialog)
        self.label.setObjectName(_fromUtf8("label"))
        self.horizontalLayout.addWidget(self.label)
        self.lineEdit = QtGui.QLineEdit(mainDialog)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.horizontalLayout.addWidget(self.lineEdit)
        self.import_2 = QtGui.QPushButton(mainDialog)
        self.import_2.setObjectName(_fromUtf8("import_2"))
        self.horizontalLayout.addWidget(self.import_2)
        self.process = QtGui.QPushButton(mainDialog)
        self.process.setObjectName(_fromUtf8("process"))
        self.horizontalLayout.addWidget(self.process)
        self.formLayout.setLayout(0, QtGui.QFormLayout.FieldRole, self.horizontalLayout)
        QtCore.QObject.connect(self.import_2,QtCore.SIGNAL("clicked()"),self.callf)


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

    def retranslateUi(self, mainDialog):
        mainDialog.setWindowTitle(_translate("mainDialog", "Fatal error check", None))
        self.label.setText(_translate("mainDialog", "Import *.f06 file", None))
        self.import_2.setText(_translate("mainDialog", "Import", None))
        self.process.setText(_translate("mainDialog", "Process", None))
    def callf(self):
        fileName = QtGui.QFileDialog.getOpenFileName(self, 'import f06 file', '/home/harisyam/Desktop', selectedFilter='*.txt')
        if fileName:
           print fileName



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    mainDialog = QtGui.QDialog()
    ui = Ui_mainDialog()
    ui.setupUi(mainDialog)
    mainDialog.show()
    sys.exit(app.exec_())

当我运行代码时,主对话框出现但是当我按下导入按钮时,文件选择器没有弹出。有谁能告诉我出了什么问题?

代码很大,因为我将.ui文件转换为.py。我已经在QT设计师中设计了gui

1 个答案:

答案 0 :(得分:1)

更改类Ui_mainDialog(object):

上课Ui_mainDialog( QtGui.QWidget ):

此外,还有拼写错误。

QtCore.QObject.connect(self.import_2,QtCore.SIGNAL( “<强> clciked ()”),self.callf)

应该点击