Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

App crashed while browsing PST files



  • Hi everyone,
    I have created a basic desktop app using Qt. It has browse button and lineedit. Whenever I press browse button it browses the files. I am able to browse all files, it is working fine. When I try to open PST files app is closing. I have tried a sample pst which is very less size of 250kb.

    def returnPressedSlot( self ):
        ''' Called when the user enters a string in the line edit and
        presses the ENTER key.
        '''
        fileName =  self.lineEdit.text()
        if self.model.isValid( fileName ):
            self.model.setFileName( self.lineEdit.text() )
            self.refreshAll()
        else:
            m = QtWidgets.QMessageBox()
            m.setText("Invalid file name!\n" + fileName )
            m.setIcon(QtWidgets.QMessageBox.Warning)
            m.setStandardButtons(QtWidgets.QMessageBox.Ok
                                 | QtWidgets.QMessageBox.Cancel)
            m.setDefaultButton(QtWidgets.QMessageBox.Cancel)
            ret = m.exec_()
            self.lineEdit.setText( "" )
            self.refreshAll()
            self.debugPrint( "Invalid file specified: " + fileName  )
    
    # slot
    def browseSlot( self ):
        ''' Called when the user presses the Browse button
        '''
        #self.debugPrint( "Browse button pressed" )
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
                        None,
                        "QFileDialog.getOpenFileName()",
                        "",
                        "All Files (*);;PST Files (*.pst)",
                        options=options)
        if fileName:
            #self.debugPrint( "setting file name: " + fileName )
            self.model.setFileName( fileName )
            self.refreshAll()
    
        print(fileName)

  • Lifetime Qt Champion

    @praneshpk So, did you use the debugger to at least find out where exactly your app is crashing?



  • @praneshpk
    Apart from @jsulm asking you where it actually "crashes". What type is self.model? And btw is the PST file you are trying to open currently in use from Outlook?



  • Yes. I am using pst which is from the current version of outlook. This is model class,

    class Model:
    	def __init__( self ):
    		'''
    		Initializes the two members the class holds:
    		the file name and its contents.
    		'''
    		self.fileName = None
    		self.fileContent = ""
    
    	def isValid( self, fileName ):
    		'''
    		returns True if the file exists and can be
    		opened.  Returns False otherwise.
    		'''
    		try: 
    			file = open( fileName, 'r' )
    			file.close()
    			return True
    		except:
    			return False
    
    	def setFileName( self, fileName ):
    		'''
    		sets the member fileName to the value of the argument
    		if the file exists.  Otherwise resets both the filename
    		and file contents members.
    		'''
    		if self.isValid( fileName ):
    			self.fileName = fileName
    			self.fileContents = open( fileName, 'r' ).read()
    		else:
    			self.fileContents = ""
    			self.fileName = ""
    			
    	def getFileName( self ):
    		'''
    		Returns the name of the file name member.
    		'''
    		return self.fileName
    
    	def getFileContents( self ):
    		'''
    		Returns the contents of the file if it exists, otherwise
    		returns an empty string.
    		'''
    		return self.fileContents

  • Lifetime Qt Champion

    @praneshpk Could you please use debugger and tell us at least where exactly it is crashing?



  • @praneshpk said in App crashed while browsing PST files:

    self.fileContents = open( fileName, 'r' ).read()

    One observation: this line seems to leave the file open from then on, unless you know better?

    Also assuming you are Windows you should open 'rb'.

    I am not saying either of these would cause a "crash".



  • @jsulm Sure



  • @JonB I have tried with 'rb', Now it is working.

    Thankyou @JonB and @jsulm



  • @jsulm @JonB I have another doubt. Now using this code I'm able to load only one file. How can I select all files or a specific folder? Can you help me with this?


  • Lifetime Qt Champion

    @praneshpk Get all files in the folder using https://doc.qt.io/qt-5/qdir.html#entryList-1 and iterate over these files...



  • Instead of

     QtWidgets.QFileDialog.getOpenFileName
    

    I have given

     QtWidgets.QFileDialog.getOpenFileNames
    

    I am able to select many files but it is not reflected in lineedit.


  • Lifetime Qt Champion

    @praneshpk said in App crashed while browsing PST files:

    I am able to select many files but it is not reflected in lineedit.

    Please show the code...



  • @praneshpk said in App crashed while browsing PST files:

    I am able to select many files but it is not reflected in lineedit.

    Well that is up to your code. getOpenFileNames() returns a list of filenames, what you do with that like putting it into a QLineEdit or QListView or whatever is up to you.



  • def returnPressedSlot( self ):
        ''' Called when the user enters a string in the line edit and
        presses the ENTER key.
        '''
        fileName =  self.lineEdit.text()
    
    
    # slot
    def browseSlot( self ):
        ''' Called when the user presses the Browse button
        '''
        #self.debugPrint( "Browse button pressed" )
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
                        None,
                        "QFileDialog.getOpenFileName()",
                        "",
                        "All Files (*);;Python Files (*.py)",
                        options=options)
        if fileName:
            #self.debugPrint( "setting file name: " + fileName )
            self.model.setFileName( fileName )
            self.refreshAll()
    

    what happens is when I use getfilename single file name is loaded and displayed. when I get many files using getfilenames it is creating a list. That list is not reflected in line edit (here linedit function is returnedpresses slot). how to get that name inside a list and display in lineedit



  • @praneshpk
    This is just a Python issue. If you have a list in Python you use join() to turn it into a string, don't you? E.g.

    print(" ".join(filenames))
    


  • @JonB have tired that it is not working


  • Lifetime Qt Champion

    @praneshpk Please show the code, not just "is not working". And in what way it did not work?



  • @jsulm I thought you would refer code that I have added at the starting of discussion. I will attach the code below.

    MyApp.py

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QObject, pyqtSlot
    from PyQt5.QtWidgets import QFileDialog, QDialog
    from mainwindow import Ui_MainWindow
    import sys
    from model import Model
    
    
    
    class MainWindowUIClass( Ui_MainWindow ):
        def __init__( self ):
            '''Initialize the super class
            '''
            super().__init__()
            self.model = Model()
            
    
        # def setupUi( self, MW ):
        #     ''' Setup the UI of the super class, and add here code
        #     that relates to the way we want our UI to operate.
        #     '''
        #     super().setupUi( MW )
        
        def refreshAll( self ):
    
            self.pstfile.setText( self.model.getFileName() )
    
        def returnPressedSlot( self ):
            ''' Called when the user enters a string in the line edit and
            presses the ENTER key.
            '''
            
            fileName =  self.lineEdit.text()
    
            if self.model.isValid( fileName ):
                self.model.setFileName( self.lineEdit.text() )
                self.refreshAll()
            else:
                m = QtWidgets.QMessageBox()
                m.setText("Invalid file name!\n" + fileName )
                m.setIcon(QtWidgets.QMessageBox.Warning)
                
                m.setStandardButtons(QtWidgets.QMessageBox.Ok
                                    | QtWidgets.QMessageBox.Cancel)
                m.setDefaultButton(QtWidgets.QMessageBox.Cancel)
                ret = m.exec_()
                self.lineEdit.setText( "" )
                self.refreshAll()
                self.debugPrint( "Invalid file specified: " + fileName  )
    
        # slot
        def browseSlot( self ):
            ''' Called when the user presses the Browse button
            '''
            #self.debugPrint( "Browse button pressed" )
            options = QtWidgets.QFileDialog.Options()
            options |= QtWidgets.QFileDialog.DontUseNativeDialog
            fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
                            None,
                            "QFileDialog.getOpenFileName()",
                            "",
                            "All Files (*);;Python Files (*.py)",
                            options=options)
            if fileName:
                #self.debugPrint( "setting file name: " + fileName )
                self.model.setFileName( fileName )
                self.refreshAll()
    
            print(fileName)
    
    
    
    
    def main():
        """
        This is the MAIN ENTRY POINT of our application.  The code at the end
        of the mainwindow.py script will not be executed, since this script is now
        our main program.   We have simply copied the code from mainwindow.py here
        since it was automatically generated by '''pyuic5'''.
    
        """
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = MainWindowUIClass()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    
    main()
    

    Model.py

    class Model:
        def __init__( self ):
            '''
            Initializes the two members the class holds:
            the file name and its contents.
            '''
            self.fileName = None
            self.fileContent = ""
    
        def isValid( self, fileName ):
            '''
            returns True if the file exists and can be
            opened.  Returns False otherwise.
            '''
            try: 
                file = open( fileName, 'rb' )
                file.close()
                return True
            except:
                return False
    
        def setFileName( self, fileName ):
            '''
            sets the member fileName to the value of the argument
            if the file exists.  Otherwise resets both the filename
            and file contents members.
            '''
            if self.isValid( fileName ):
                self.fileName = fileName
                self.fileContents = open( fileName, 'rb' ).read()
            else:
                self.fileContents = ""
                self.fileName = ""
                
        def getFileName( self ):
            '''
            Returns the name of the file name member.
            '''
            return self.fileName
    
        def getFileContents( self ):
            '''
            Returns the contents of the file if it exists, otherwise
            returns an empty string.
            '''
            return self.fileContents
    

    mainwindow.py

    import os 
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QObject, pyqtSlot
    
    class Ui_MainWindow(QObject):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(805, 723)
            MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.Title = QtWidgets.QLabel(self.centralwidget)
            self.Title.setGeometry(QtCore.QRect(20, 10, 381, 71))
            font = QtGui.QFont()
            font.setFamily("Arial Black")
            font.setPointSize(20)
            font.setBold(True)
            font.setWeight(75)
            self.Title.setFont(font)
            self.Title.setObjectName("Title")
            self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget.setGeometry(QtCore.QRect(200, 120, 431, 71))
            self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setObjectName("verticalLayout")
            self.pstfile = QtWidgets.QLineEdit(self.verticalLayoutWidget)
            self.pstfile.setObjectName("pstfile")
            self.verticalLayout.addWidget(self.pstfile)
            self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(640, 120, 141, 71))
            self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
            self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.browse = QtWidgets.QPushButton(self.verticalLayoutWidget_2)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.browse.setFont(font)
            self.browse.setObjectName("browse")
            self.verticalLayout_2.addWidget(self.browse)
            self.verticalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(20, 120, 171, 71))
            self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
            self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
            self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_3.setObjectName("verticalLayout_3")
            self.label = QtWidgets.QLabel(self.verticalLayoutWidget_3)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(9)
            font.setBold(True)
            font.setWeight(75)
            self.label.setFont(font)
            self.label.setAlignment(QtCore.Qt.AlignCenter)
            self.label.setObjectName("label")
            self.verticalLayout_3.addWidget(self.label)
            self.verticalLayoutWidget_4 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_4.setGeometry(QtCore.QRect(200, 240, 431, 171))
            self.verticalLayoutWidget_4.setObjectName("verticalLayoutWidget_4")
            self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_4)
            self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_4.setObjectName("verticalLayout_4")
            self.frommtext = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.frommtext.setObjectName("fromtext")
            self.verticalLayout_4.addWidget(self.frommtext)
            self.totext = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.totext.setObjectName("totext")
            self.verticalLayout_4.addWidget(self.totext)
            self.subjecttext = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.subjecttext.setObjectName("subjecttext")
            self.verticalLayout_4.addWidget(self.subjecttext)
            self.domaintext = QtWidgets.QTextEdit(self.verticalLayoutWidget_4)
            self.domaintext.setObjectName("domaintext")
            self.verticalLayout_4.addWidget(self.domaintext)
            self.run = QtWidgets.QPushButton(self.centralwidget)
            self.run.setGeometry(QtCore.QRect(340, 500, 131, 51))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(14)
            font.setBold(True)
            font.setWeight(75)
            self.run.setFont(font)
            self.run.setLayoutDirection(QtCore.Qt.LeftToRight)
            self.run.setObjectName("run")
            self.verticalLayoutWidget_5 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_5.setGeometry(QtCore.QRect(20, 240, 171, 171))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.verticalLayoutWidget_5.setFont(font)
            self.verticalLayoutWidget_5.setObjectName("verticalLayoutWidget_5")
            self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_5)
            self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_5.setObjectName("verticalLayout_5")
            self.fromm = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.fromm.setFont(font)
            self.fromm.setAlignment(QtCore.Qt.AlignCenter)
            self.fromm.setObjectName("from")
            self.verticalLayout_5.addWidget(self.fromm)
            self.to = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.to.setFont(font)
            self.to.setAlignment(QtCore.Qt.AlignCenter)
            self.to.setObjectName("to")
            self.verticalLayout_5.addWidget(self.to)
            self.subject = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.subject.setFont(font)
            self.subject.setAlignment(QtCore.Qt.AlignCenter)
            self.subject.setObjectName("subject")
            self.verticalLayout_5.addWidget(self.subject)
            self.domain = QtWidgets.QLabel(self.verticalLayoutWidget_5)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.domain.setFont(font)
            self.domain.setAlignment(QtCore.Qt.AlignCenter)
            self.domain.setObjectName("domain")
            self.verticalLayout_5.addWidget(self.domain)
            self.verticalLayoutWidget_6 = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget_6.setGeometry(QtCore.QRect(640, 230, 141, 191))
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.verticalLayoutWidget_6.setFont(font)
            self.verticalLayoutWidget_6.setObjectName("verticalLayoutWidget_6")
            self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_6)
            self.verticalLayout_6.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_6.setObjectName("verticalLayout_6")
            self.frombutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.frombutton.setFont(font)
            self.frombutton.setObjectName("frombutton")
            self.verticalLayout_6.addWidget(self.frombutton)
            self.tobutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.tobutton.setFont(font)
            self.tobutton.setObjectName("tobutton")
            self.verticalLayout_6.addWidget(self.tobutton)
            self.subjectbutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.subjectbutton.setFont(font)
            self.subjectbutton.setObjectName("subjectbutton")
            self.verticalLayout_6.addWidget(self.subjectbutton)
            self.domainbutton = QtWidgets.QPushButton(self.verticalLayoutWidget_6)
            font = QtGui.QFont()
            font.setFamily("Arial")
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.domainbutton.setFont(font)
            self.domainbutton.setObjectName("domainbutton")
            self.verticalLayout_6.addWidget(self.domainbutton)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 805, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            self.browse.clicked.connect(self.browseSlot)
            self.pstfile.returnPressed.connect(self.returnPressedSlot)
            self.run.clicked.connect(self.runSlot)
            self.frombutton.clicked.connect(self.button_click1)
            self.tobutton.clicked.connect(self.button_click2)
            self.subjectbutton.clicked.connect(self.button_click3)
            self.domainbutton.clicked.connect(self.button_click4)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.Title.setText(_translate("MainWindow", "PST Mail Analyzer"))
            self.browse.setText(_translate("MainWindow", "Browse PST"))
            self.label.setText(_translate("MainWindow", "Select the PST file/folder"))
            self.run.setText(_translate("MainWindow", "RUN"))
            self.fromm.setText(_translate("MainWindow", "From"))
            self.to.setText(_translate("MainWindow", "To"))
            self.subject.setText(_translate("MainWindow", "Subject / Body"))
            self.domain.setText(_translate("MainWindow", "Domain"))
            self.frombutton.setText(_translate("MainWindow", "Load"))
            self.tobutton.setText(_translate("MainWindow", "Load"))
            self.subjectbutton.setText(_translate("MainWindow", "Load"))
            self.domainbutton.setText(_translate("MainWindow", "Load"))
    
    
    
    
        @pyqtSlot( )
        def browseSlot( self ):
            pass
    
        @pyqtSlot( )
        def returnPressedSlot( self ):
            pass
    
    
                
        @pyqtSlot( )
        def button_click1(self):
            fromlabel = self.frommtext.toPlainText()
            print (fromlabel) 
            
        @pyqtSlot( )
        def button_click2(self):
            tolabel = self.totext.toPlainText()
            print(tolabel)
            
        @pyqtSlot( )
        def button_click3(self):
            subjectlabel = self.subjecttext.toPlainText()
            print(subjectlabel)
            
        @pyqtSlot( )
        def button_click4(self):
            domainlabel = self.domaintext.toPlainText()
            print(domainlabel)
    
        @pyqtSlot( )
        def runSlot( self ):
            os.system('python run.py')
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())

  • Lifetime Qt Champion

    @praneshpk Where in this code do you get a list of files, do the join (as @JonB suggested) and then set the label?!
    I only see your old QtWidgets.QFileDialog.getOpenFileName to get ONE file, not a list?
    Did you try it at all?!
    I also asked only for the relevant code, not the whole code!


Log in to reply