PyQt5 QFileDialog finishes app (when called from child window)



  • 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 :))


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • @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.


  • Lifetime Qt Champion

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



  • @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?



  • @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.



  • @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.



  • @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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.