无法在主框架中获取lineEdit进行更新

时间:2016-09-26 15:27:12

标签: python-2.7 pyqt4 qlineedit

我有一个主窗口,其中有一个选项选项卡,允许用户更新其输入文件和主目录。此功能为用户可为项目创建的每个给定配置文件更新文本文件。所有这些函数都很有效.. lineEdits在__init__中首次执行时显示正确的目录...除了在进行更改时将更改反映到主窗口上的lineEdits之外。

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)

我认为最后一个函数,而不是进入主窗口是创建主窗口的另一个实例,但没有显示cuz我没有用main.show()告诉它,但我不知道如何发送右键信号到主GUI刷新lineEdits ...我知道文本被设置cuz控制台正在打印lineEdits更改为正确的目录但lineEdits本身不反映。我尝试使用QApplication.instance().processEvents(),但我认为它不起作用。我将它与update_dir_lines函数一起使用,它只是终止了主GUI。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

当您使用exec_()打开对话框时,它将会阻止,直到用户关闭它。所以解决方案很简单:

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()

不再需要go2main()方法,因此您可以这样做:

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()