0

I want my pyqt application to act as a file handler for a certain file format (with a certain suffix), that is, I want to be able to open files from the operating system directly in my pyqt application, say by double clicking or contextual menu "open with".
Is this possible?

Thank you and best regards!

ytti
  • 1
  • See http://stackoverflow.com/questions/15603334/register-a-python-script-as-default-for-file-type – three_pineapples Oct 15 '14 at 08:35
  • Take a look here http://stackoverflow.com/questions/3235221/how-to-add-command-to-right-click-menu-of-certain-extension-programmatically and http://stackoverflow.com/questions/6716200/how-to-open-a-particular-file-with-my-qt-application-on-mac-when-i-double-click/6716340#6716340 – Pratham Oct 15 '14 at 08:38
  • thank you! sorry, just could not find these answers myself :/ – ytti Oct 15 '14 at 11:38

1 Answers1

0

Yes, it is possible. I suggest using the QFileDialog.getOpenFileName()

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog


class MainWin(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(150,150,200,150)
        self.setWindowTitle("Test")

        button = QPushButton('Open with', self)
        button.clicked.connect(self.dialog)
        button.resize(100,32)
        button.move(50,50)

        self.show()

    def dialog(self):
        # https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QFileDialog.html#PySide2.QtWidgets.PySide2.QtWidgets.QFileDialog.getOpenFileName
        file, check = QFileDialog.getOpenFileName(
            None,
            "QFileDialog.getOpenFileName()",
            "",
            self.tr(
                'All Files (*);;'
                'Python Files (*.py);;'
                'Text Files (*.txt)'))

        if check:
            print(file)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MainWin()
    sys.exit( app.exec_() )
cigien
  • 57,834
  • 11
  • 73
  • 112