Search code examples
python-2.7pyqt4qlineedit

Cannot get lineEdit to update in main frame


I have a main window that has an options tab that allows a user to update their input file and home directories. This function updates a text file for each of the given profiles a user could have for a project. All these functions work great..the lineEdits show the right directories when first executed in the __init__ ...except for the fact of reflecting the changes onto the lineEdits on the main window when a change has been made.

class MAIN_GUI(QtGui.QMainWIndow):
    def __init__(self):
        super(MAIN_GUI,self).__init__()
        self.uiM=Ui_MainWindow()
        self.uiM.setupUi(self)
        self.update_dir_lines()
        self.connect(self.uiM.Options_ChangeButton,QtCore.SIGNAL("clicked()"),self.choose_DIR)

    def choose_DIR(self):
        choose_DIR = Choose_DIR(current_profile,'main')
        choose_DIR.show()
        choose_DIR.exec_()

    def update_dir_lines(self):
        #pull the saved file directory from the profile.txt
        self.InputFileDir = str(profile.get_dir('InputFileDir'))
        self.uiM.Options_InputDIR_lineEdit.setText(self.InputFileDir)
        #just to see that it's been changed print into console
        print self.uiM.Options_inputDIR_lineEdit.text() #this actually comes back with the changed directory
        #same spiel for the home directory so no need to repeat

class Choose_DIR(QtGui.QDialog):
    def __init__(self):
        super(Choose_DIR,self).__init__()
        self.ui3=Ui_Directories()
        self.ui3.setupUi(self)
        self.connect(self.ui3.Options_InputFileDir_BrowseButton,QtCore.SIGNAL("clicked()"),self.get_InputFileDir)
        #doing the same for the home dir so no need to repeat

    def get_InputFileDir(self):
        QString_dir = QFileDialog.getExistingDirectory(parent=None,caption=("Choose InputFile Directory"),directory(''),optins=QFileDialog.ShowDirsOnly)
        self.ui3.Options_InputFileDir_lineEdit.setText(QString_dir) #this works perfectly reflecting the chosen input File Dir
        self.connect(self.ui3.select_DIR_ExecuteButton,QtCore.SIGNAL("clicked()"),self.change_DIR)
    def change_DIR(self):
        InputFileDir = self.ui3.Options_InputFileDir_lineEdit.text()
        #profile is being edited and saved
        profile.change_dir('InputFileDir',InputFileDir) 
        self.go2main()

    def go2main(self):
        self.close()
        main = MAIN_GUI()
        QtCore.QTimer.singleShot(1000,main.update_dir_lines)

I think the last function, instead of going to the main window is creating another instance of the main window but not showing cuz I haven't told it with main.show() but I don't know how to send the right signal to the main GUI to refresh the lineEdits...I know the text were set cuz the console is printing out the correct directories the lineEdits were changed to but the lineEdits themselves do not reflect that. I tried using QApplication.instance().processEvents() but I don't think it's working. I used it with the update_dir_lines function and it just terminated the main GUI. Can anyone help me out?


Solution

  • When you open a dialog with exec_(), it will block until the user closes it. So the solution is quite simple:

    class MAIN_GUI(QtGui.QMainWIndow):
        ...
    
        def choose_DIR(self):
            choose_DIR = Choose_DIR(current_profile,'main')
            if choose_DIR.exec_() == QtGui.QDialog.Accepted:
                self.update_dir_lines()
    

    The go2main() method is no longer needed, so you can just do:

    class Choose_DIR(QtGui.QDialog):
        ...
    
        def change_DIR(self):
            InputFileDir = self.ui3.Options_InputFileDir_lineEdit.text()
            #profile is being edited and saved
            profile.change_dir('InputFileDir',InputFileDir) 
            self.accept()