PyQt QtGui.QFileDialog 不工作?

PyQt QtGui.QFileDialog not Working?

我正在 Python2.7 中使用 Pyqt4 构建用户界面,但是...当我单击“保存”按钮时,总是出现类型错误,即

TypeError: QFileDialog.getSaveFileName(QWidget parent=None, QString caption=QString(), QString directory=QString(), QString filter=QString(), QString selectedFilter=None, QFileDialog.Options options=0): argument 1 has unexpected type 'Ui_MainWindow'

我的代码如下:

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(640, 400)
    #code-skipped



def save(self):
    filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
    f = open(filename, 'w')
    filedata = self.textEdit.toPlainText()
    f.write(filedata)
    f.close()

def retranslateUi(self, MainWindow):
   #code-Skipped

class FileDialog(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
    QtGui.QMainWindow.__init__(self)
    self.setupUi(self)

def browse(self):
    filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
    fname = open(filename)
    data = fname.read()
    self.textEdit.setText(data)
    fname.close()

if __name__ == '__main__' :
app = QtGui.QApplication(sys.argv)

mainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())

请帮助我,代码片段已被删除...

UI_Mainwindow 不是 QtGui.QWidget 的实例。 使用

 filename = QtGui.QFileDialog.getSaveFileName(None, 'Save File', os.getenv('HOME'))

def setupUi(self, MainWindow):
    self.window = MainWindow
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(640, 400)
    #code-skipped

def save(self):
    filename = QtGui.QFileDialog.getSaveFileName(self.window, 'Save File', os.getenv('HOME'))

将 QWidget 实例作为 QFileDialog 的父级传递。