0

My qt application runs a process and saves the results in a particular directory.

Now user has to go to the folder for viewing the results. Generally will be a excel file.

I have tried getExistingDirectory/getOpenFileName where it opens the dir path for choosing the folder or opening the file.

I just want to open a folder independent of pyqt4. (For ex : Pressing "Windows + E" shortcut key in windows.

from PyQt4 import QtGui,QtCore
import sys,os

class OpenDir(QtGui.QMainWindow):
    def __init__(self):
        super(OpenDir, self).__init__()
        # self.openDirectory()
        self.button = QtGui.QPushButton('Open', self)
        self.button.clicked.connect(self.openDirectory)
        self.setCentralWidget(self.button)

    def openDirectory(self):
        print "Hi i am openDirectory Function . I will open Directory selected "
        openDirectoryDialog=QtGui.QFileDialog()
        print openDirectoryDialog

        # oD=openDirectoryDialog.getExistingDirectory(self,"open",os.path.abspath("..\."),openDirectoryDialog.ShowDirsOnly)  #Selectes folder
        # oD = openDirectoryDialog.directoryEntered(self,"open", os.path.abspath("..\."))
        print oD


        if len(oD) > 0:
            print "accepted"
        else:
            print "nothing selected"


def main():
    app = QtGui.QApplication(sys.argv)
    ui=OpenDir()
    ui.show()
    sys.exit(app.exec_())
#Function Main END

if __name__ == '__main__':
    main()
PAR
  • 624
  • 1
  • 5
  • 16
  • You are probably looking for the answer to this question: [How to “Reveal in Finder” or “Show in Explorer” with Qt](http://stackoverflow.com/q/3490336/507028). The answer is for C++ but easily adaptable to python. If you are only targeting Windows without portability, you can start explorer.exe with this command `C:\Windows\explorer.exe /select,`. – Neitsa Jan 06 '17 at 13:35
  • @Neitsa: Thankyou, it really helped. But i have been looking for a way within a pyqt QfileDialog or any other module from pyqt. – PAR Jan 07 '17 at 04:31

1 Answers1

0

You can use key press event for this. Here is an example with F1

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys,os

class OpenDir(QMainWindow):
    def __init__(self):
        super(OpenDir, self).__init__()
        # self.openDirectory()
        self.button = QPushButton('Open', self)
        self.button.clicked.connect(self.openDirectory)
        self.setCentralWidget(self.button)

    def openDirectory(self):
        print "Hi i am openDirectory Function . I will open Directory selected "
        self.openDirectoryDialog=ddir = QFileDialog.getExistingDirectory(self, "Get Dir Path")
        print self.openDirectoryDialog

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_F1:
            os.system('xdg-open "%s"' % self.openDirectoryDialog)

def main():
    app = QApplication(sys.argv)
    ui=OpenDir()
    ui.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Oxymoron88
  • 495
  • 3
  • 9
  • Thankyou. xdg-open is a linux command. However it worked in linux. – PAR Jan 07 '17 at 04:33
  • You can actually standardise this for all OS in a similar way: http://stackoverflow.com/questions/1679798/how-to-open-a-file-with-the-standard-application – Oxymoron88 Jan 07 '17 at 04:56