Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PyQt5 QFileDialog finishes app (when called from child window)
QtWS25 Last Chance

PyQt5 QFileDialog finishes app (when called from child window)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexander_Moscow
    wrote on last edited by Alexander_Moscow
    #1

    Sorry, that asking my question here...
    Maybe someone, who works with PyQt5, helps me...
    I have problem with crashing my application after I call QFileDialog.

    I have a bit complex structure of my application.

    First_Main_Window(QtWidgets.QMainWindow) calls ---> Second_Main_Window(QtWidgets.QMainWindow) calls ---> Third_Window = slides_form (QtWidgets.QDialog)

    It means in First Window I have list of workers, in second window - data of some worker (Phone, address) in third window - standard stages of work (i.e. photos) the worker does..

    The 'First_Main_Window' opens Second_Main_Window, using this function:

    def OpenWindowCurrentWorker(self):
        self.pasportdataWin = pasportdata.pasportdata_form(self)
        self.pasportdataWin.closed.connect(self.show)
        self.pasportdataWin.move(0,0)
        self.hide()
        self.pasportdataWin.show()
    

    The Second_Main_Window opens Third_Window, using this function:

    def OpenSlidesWindowForm(self):
        self.SlidesWin=slides.slides_form(self)
        self.SlidesWin.move(0,0)
        self.SlidesWin.closed.connect(self.show)
        self.hide()
        self.SlidesWin.show()
    

    The third window has this code:

    from PyQt5 import QtCore, QtWidgets, QtGui
    class slides_form (QtWidgets.QDialog):
        closed = QtCore.pyqtSignal()
        def __init__(self,parent=None):
            super(slides_form, self).__init__(parent)
            self.ui = slidesUiForm.Ui_Form()
            self.ui.setupUi(self)
            self.Parent=parent
            self.ui.pushButton.clicked.connect(self.openNewSlideFile)
    
        @QtCore.pyqtSlot()
        def openNewSlideFile (self):
            Slideflnames = QtWidgets.QFileDialog.getOpenFileNames(self, "Open Image file", "All images(*.png *.gif *.jpg *jpeg *.bmp *.tiff   .tif)")[0]
            for ii in range(len(Slideflnames)):
                   ........
                shutil.copy(os.path.abspath(Slideflnames[ii]), NEWPATH)
    

    The problem is that I decided to move from PyQt4, where all worked fine, to PyQt5.
    And the problem is that after calling procedure "openNewSlideFile" application closes without any warnings and errors. It looks like normal exit from application.

    Any call to QtWidgets.QFileDialog.getOpenFileNames from the second and third windows finishes the application. I tried to use "None" instead of "self" in parent position, but it didn't help.

    I have to emphasize, that my function "openNewSlideFile" works till it's end and crash appears at the moment of exiting this function at its end, or when I write command "return" just after calling for QtWidgets.QFileDialog.getOpenFileName(s)

    If I call self.setParent(None) in def init procedure of last window - application doesn't crash after completing function, containing QFileDialog.getOpenFileName(s), but I cannot return back to Window_2

    !!!!
    I found some kind of answer to my question - in my case every opened "child" window hides previous "parent window" when initialized and shows "parent" back when closes - in order to minimize quantity of widgets in status string of desktop

    If I remove self.hide() when I call for "second window" all works fine! But! I have two windows tabs in status string (I forget english word to describe it exactly :) )
    One window is inactive because active window is modal... But It looks like some unnecessary garbage and annoying me :))

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you try to use pdb to see what happens with your application ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        Did you try to use pdb to see what happens with your application ?

        A Offline
        A Offline
        Alexander_Moscow
        wrote on last edited by Alexander_Moscow
        #3

        @SGaist Thank you for answer!
        When I start script via pure python3 it looks like no errors there - just spontaneous finishing of application.
        if i start usin pdb, a segmentation error appears at the end.
        here is console output:

        [alex@comp projects_PyQt5]$ python3
        Python 3.5.1 (default, May  5 2016, 10:50:17) 
        [GCC 5.3.1 20151207 (ALT Linux 5.3.1-alt3)] on linux
        Type "help", "copyright", "credits" or "license" for more information.
        >>> 
        [1]+  Stopped                 python3
        [alex@comp projects_PyQt5]$ python3 BaseCenter.py
        [alex@comp projects_PyQt5]$ python3 -m pdb BaseCenter.py
        > /home/alex/projects_PyQt5/BaseCenter.py(3)<module>()
        -> import os
        (Pdb) continue
        QXcbConnection: XCB error: 3 (BadWindow), sequence: 2466, resource id: 39846744, major code: 40 (TranslateCoords), minor code: 0
        The program exited via sys.exit(). Exit status: 0
        > /home/alex/projects_PyQt5/BaseCenter.py(3)<module>()
        -> import os
        (Pdb) continue
        Segmentation error
        [alex@comp projects_PyQt5]$
        

        PS. I've found some answer - put it in start message.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you give a minimal complete example that shows the behaviour ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • A Alexander_Moscow

            Sorry, that asking my question here...
            Maybe someone, who works with PyQt5, helps me...
            I have problem with crashing my application after I call QFileDialog.

            I have a bit complex structure of my application.

            First_Main_Window(QtWidgets.QMainWindow) calls ---> Second_Main_Window(QtWidgets.QMainWindow) calls ---> Third_Window = slides_form (QtWidgets.QDialog)

            It means in First Window I have list of workers, in second window - data of some worker (Phone, address) in third window - standard stages of work (i.e. photos) the worker does..

            The 'First_Main_Window' opens Second_Main_Window, using this function:

            def OpenWindowCurrentWorker(self):
                self.pasportdataWin = pasportdata.pasportdata_form(self)
                self.pasportdataWin.closed.connect(self.show)
                self.pasportdataWin.move(0,0)
                self.hide()
                self.pasportdataWin.show()
            

            The Second_Main_Window opens Third_Window, using this function:

            def OpenSlidesWindowForm(self):
                self.SlidesWin=slides.slides_form(self)
                self.SlidesWin.move(0,0)
                self.SlidesWin.closed.connect(self.show)
                self.hide()
                self.SlidesWin.show()
            

            The third window has this code:

            from PyQt5 import QtCore, QtWidgets, QtGui
            class slides_form (QtWidgets.QDialog):
                closed = QtCore.pyqtSignal()
                def __init__(self,parent=None):
                    super(slides_form, self).__init__(parent)
                    self.ui = slidesUiForm.Ui_Form()
                    self.ui.setupUi(self)
                    self.Parent=parent
                    self.ui.pushButton.clicked.connect(self.openNewSlideFile)
            
                @QtCore.pyqtSlot()
                def openNewSlideFile (self):
                    Slideflnames = QtWidgets.QFileDialog.getOpenFileNames(self, "Open Image file", "All images(*.png *.gif *.jpg *jpeg *.bmp *.tiff   .tif)")[0]
                    for ii in range(len(Slideflnames)):
                           ........
                        shutil.copy(os.path.abspath(Slideflnames[ii]), NEWPATH)
            

            The problem is that I decided to move from PyQt4, where all worked fine, to PyQt5.
            And the problem is that after calling procedure "openNewSlideFile" application closes without any warnings and errors. It looks like normal exit from application.

            Any call to QtWidgets.QFileDialog.getOpenFileNames from the second and third windows finishes the application. I tried to use "None" instead of "self" in parent position, but it didn't help.

            I have to emphasize, that my function "openNewSlideFile" works till it's end and crash appears at the moment of exiting this function at its end, or when I write command "return" just after calling for QtWidgets.QFileDialog.getOpenFileName(s)

            If I call self.setParent(None) in def init procedure of last window - application doesn't crash after completing function, containing QFileDialog.getOpenFileName(s), but I cannot return back to Window_2

            !!!!
            I found some kind of answer to my question - in my case every opened "child" window hides previous "parent window" when initialized and shows "parent" back when closes - in order to minimize quantity of widgets in status string of desktop

            If I remove self.hide() when I call for "second window" all works fine! But! I have two windows tabs in status string (I forget english word to describe it exactly :) )
            One window is inactive because active window is modal... But It looks like some unnecessary garbage and annoying me :))

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Alexander_Moscow said in PyQt5 QFileDialog finishes app (when called from child window):

                Slideflnames = QtWidgets.QFileDialog.getOpenFileNames(self, "Open Image file", "All images(*.png *.gif *.jpg *jpeg *.bmp *.tiff   .tif)")[0]
                for ii in range(len(Slideflnames)):
                    shutil.copy(os.path.abspath(Slideflnames[ii]), NEWPATH)
            

            I have no idea how this might relate to your dialog structure.

            But are you aware that in PyQt5 QFileDialog.getOpenFileName() does not return same value as it did at PyQt4 ? It's very silly. Here is the code I use, with comments to explain:

            def fileDialogGetOpenFileName(parent: typing.Optional[QWidget] = None, caption: str = None, directory: str = None,
                                          filter: str = None, initialFilter: str = None,
                                          options: typing.Union['QFileDialog.Options', 'QFileDialog.Option'] = QFileDialog.Option(0)
                                          ) -> str:
                # Replacement for QFileDialog.getOpenFileName()
                # which at PyQt5 returns a tuple
                # (see https://riverbankcomputing.com/pipermail/pyqt/2014-March/033923.html,
                #  http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#qfiledialog,
                #  also http://pyqt.sourceforge.net/Docs/PyQt5/gotchas.html)
                # which seems odd/useless/barely documented
                # so this function returns just the filename, cf. http://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
                filename, _ = QFileDialog.getOpenFileName(parent, caption, directory, filter, initialFilter, options)
                return filename
            

            My code is for getOpenFileName() (singular), but I would guess it applies for getOpenFileNames() (plural) too. (BTW, also applies to, say, getSaveFileName().)

            So as it stands now your code will pass nasty things from Slideflnames[ii] to other functions, could that be why you are crashing?

            A 1 Reply Last reply
            1
            • JonBJ JonB

              @Alexander_Moscow said in PyQt5 QFileDialog finishes app (when called from child window):

                  Slideflnames = QtWidgets.QFileDialog.getOpenFileNames(self, "Open Image file", "All images(*.png *.gif *.jpg *jpeg *.bmp *.tiff   .tif)")[0]
                  for ii in range(len(Slideflnames)):
                      shutil.copy(os.path.abspath(Slideflnames[ii]), NEWPATH)
              

              I have no idea how this might relate to your dialog structure.

              But are you aware that in PyQt5 QFileDialog.getOpenFileName() does not return same value as it did at PyQt4 ? It's very silly. Here is the code I use, with comments to explain:

              def fileDialogGetOpenFileName(parent: typing.Optional[QWidget] = None, caption: str = None, directory: str = None,
                                            filter: str = None, initialFilter: str = None,
                                            options: typing.Union['QFileDialog.Options', 'QFileDialog.Option'] = QFileDialog.Option(0)
                                            ) -> str:
                  # Replacement for QFileDialog.getOpenFileName()
                  # which at PyQt5 returns a tuple
                  # (see https://riverbankcomputing.com/pipermail/pyqt/2014-March/033923.html,
                  #  http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#qfiledialog,
                  #  also http://pyqt.sourceforge.net/Docs/PyQt5/gotchas.html)
                  # which seems odd/useless/barely documented
                  # so this function returns just the filename, cf. http://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
                  filename, _ = QFileDialog.getOpenFileName(parent, caption, directory, filter, initialFilter, options)
                  return filename
              

              My code is for getOpenFileName() (singular), but I would guess it applies for getOpenFileNames() (plural) too. (BTW, also applies to, say, getSaveFileName().)

              So as it stands now your code will pass nasty things from Slideflnames[ii] to other functions, could that be why you are crashing?

              A Offline
              A Offline
              Alexander_Moscow
              wrote on last edited by
              #6

              @JonB Thank you for reaction!
              This is not the problem in my case - if you look at the string, containing call for QFileDialog - you'll see "[0]" at its end. So I get true result.

              JonBJ 1 Reply Last reply
              1
              • A Alexander_Moscow

                @JonB Thank you for reaction!
                This is not the problem in my case - if you look at the string, containing call for QFileDialog - you'll see "[0]" at its end. So I get true result.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Alexander_Moscow said in PyQt5 QFileDialog finishes app (when called from child window):

                @JonB Thank you for reaction!
                This is not the problem in my case - if you look at the string, containing call for QFileDialog - you'll see "[0]" at its end. So I get true result.

                Wow! On my screen that [0] was just scrolled out of view! Sorry, good that you knew that change PyQt4->5.

                1 Reply Last reply
                1
                • SGaistS SGaist

                  Can you give a minimal complete example that shows the behaviour ?

                  A Offline
                  A Offline
                  Alexander_Moscow
                  wrote on last edited by
                  #8

                  @SGaist Thank you for reaction!
                  I will try to compose minimal complete example. But this "bug" is temporarily solved at this moment, so now I have to think about other questions in my script :-) Thank you very much that you spend your time helping me!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved