2

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Omar Abo Elsoud
  • 154
  • 2
  • 12

1 Answers1

2

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for your reply , I would like to clarify something . I ready implemented pushing and connect function that call openfileX in class C , but I can not understand where I should implement mainWindow ; however, i copied the code of on_clicked(self) function in openFileX function and I had to remove @ symbol from @Qtcore.pyqtSlot() to make it work , but still everytime I reopen from it opens on the root directory – Omar Abo Elsoud Dec 16 '18 at 09:32
  • @OmarAboElsoud It seems to me that he has implemented it incorrectly, if you want help you should provide a [mcve] – eyllanesc Dec 16 '18 at 14:51
  • @OmarAboElsoud Have you tried my example? If my example works then it supports my idea that it has been implemented incorrectly in its own code. – eyllanesc Dec 16 '18 at 14:52
  • I think my illustration for my example was kind of poor , but I have tried to run your code, it didn't show any output . – Omar Abo Elsoud Dec 18 '18 at 12:09
  • @OmarAboElsoud My goal is not to show the output but to show the functionality you want. Where do you want to observe the output? – eyllanesc Dec 18 '18 at 12:27