Controlling PyQt4 QFileDialog

12 Views Asked by At

This code creates a Window with a button, that Shows the Windows QFileDialog:

import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QComboBox, QProgressBar, QFileDialog
from PyQt4.QtCore import QSize, pyqtSlot, QCoreApplication


class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()
        self.setGeometry(500, 300, 820, 350)
        self.setWindowTitle("Program")
        self.initUI()

    def initUI(self):

        #Buttons
        btnposx = 30
        btnposy = 50

        self.btn1 = QPushButton('button 1', self)
        self.btn1.move(btnposx,btnposy)
        self.btn1.resize(180,30)
        self.btn1.clicked.connect(self.fp1)

        #LineEdits
        leposx = 230
        leposy = btnposy


        self.le1 = QLineEdit('Filepath',self)
        self.le1.move(leposx,leposy)
        self.le1.resize(550,26)        



        self.show()

    @pyqtSlot()
    def fp1(self):
        fp1 = QFileDialog.getOpenFileName(self, 'Open File')
        self.le1.setText(fp1)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

There is one Problem. When I open the QFileDialog and Close it without selecting a file, the string inside the QLineEdit is deleted. But I want it to stay. How do I do that?

0

There are 0 best solutions below