Omar Abo Elsoud
Omar Abo Elsoud

Reputation: 314

PyQt5 reopen on the same directory I visited

I am using getOpenFileName to open file , I have 3 class that I bind between them in the main . For example ,

Class A () Class B () Class C () Main()

Main it showing window which have 3 pushing buttons : each button is calling one of three classes , and each one open another window responsible on its own function ; however, class C is responsible on getting files from directory .

What I want to do is that make getOpenFileName remember the last visited directory even after I close class's window , but still the main is running . in other words ,cache file path which I opened last time .

The code below for more illustration .

Class C():

def OpenFileX(self):
    self.file, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', QtCore.QDir.rootPath() , '*.csv')
    self.textBrowserMS.setText(self.fileName)
    return self.fileName

def getfileOG(self):
    filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', QtCore.QDir.rootPath() , '*.csv')
    self.textBrowserOG.setText(filePath)
def getfileConfig(self):
    filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', QtCore.QDir.rootPath() , '*.csv')
    self.textEdit_config.setText(filePath) 

Main Class

Import C
class analysis(QtWidgets.QMainWindow, C.Ui_Dialog):
    def __init__(self,parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        #self.ui = C.Ui_MainWindow()
        self.setupUi(self)

Any ideas How I can do that

Upvotes: 2

Views: 883

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

You have to save the last path in persistent memory, for example with QSettings and for this you must set a setOrganizationName(), setOrganizationDomain() and setApplicationName().

from PyQt5 import QtCore, QtWidgets

class C(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(C, self).__init__(parent)
        self.te = QtWidgets.QTextEdit()
        button = QtWidgets.QPushButton("Press me")
        button.clicked.connect(self.on_clicked)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.te)
        lay.addWidget(button)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        settings = QtCore.QSettings()
        path = settings.value("Paths/csvfile", QtCore.QDir.rootPath())
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', path, '*.csv')
        if filename:
            self.te.setText(filename)
            finfo = QtCore.QFileInfo(filename)
            settings.setValue("Paths/csvfile", finfo.absoluteDir().absolutePath())

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.c = C()
        button = QtWidgets.QPushButton("Open C Dialog")
        button.clicked.connect(self.c.show)
        self.setCentralWidget(button)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    QtCore.QCoreApplication.setOrganizationName("MySoft")
    QtCore.QCoreApplication.setOrganizationDomain("mysoft.com")
    QtCore.QCoreApplication.setApplicationName("MyApp")
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions