PyQt file browsing - setting a default option?

喜夏-厌秋 提交于 2019-12-02 14:44:39

问题


I've been trying to implement a file browsing widget in the GUI I'm designing. I am using the QFileDialog module, which works great - I can browse and save a file with the following line of code:

filenames = QFileDialog.getOpenFileName()

My widget is set up with a QLineEdit, which I would like to display the name of the file selected, and a QPushButton, which I would like to initiate the above line of code. However, I'd like to know if there's a way I can set a "default" option. If the browse push button is not clicked, I would like the file to be the following:

filenames = str(glob.glob('*.npy')[0])

Which would be saved as the filename in question and show up in my LineEdit. My problem is coming from trying to display a different file name in the LineEdit, depending on whether or not the browse push button has been clicked. If it has been clicked, I would like the LineEdit to show the user-selected file instead of the default option. Here are the applicable lines of code in my retranslate function:

def retranslateUi(self, ROIGUI):
    self.lineEdit.setText(_translate("ROIGUI", self.fileSelect(False), None))
    self.Browse.setText(_translate("ROIGUI", "Browse...", None))
    self.Browse.clicked.connect(self.fileSelect(True))

Which link to the following function. As you can see, this is currently not working correctly because in the LineEdit, tripped is always False. Very silly.

def fileSelect(self,tripped):
    filenames = str(glob.glob('*.npy')[0])
    if tripped==True:
        filenames = QFileDialog.getOpenFileName()
        self.lineEdit.setText(_translate("ROIGUI", filenames, None))
    return filenames

I've been trying different ways of getting this to work, but everything I tried either (a) never updates my LineEdit after file browsing, or (b) runs file browsing immediately without ever using the default option. Thoughts? I'm sure there's a way of doing this that I'm just not seeing.

Thank you in advance.

Edited To Add

I think I have fixed most of my problem - my Browse button is now connected via buttonGroup to an integer, so my fileSelect looks like this:

def fileSelect(self):
    signal = self.buttonGroup2.checkedId()
    if signal==-1:
        filenames = str(glob.glob('*.npy')[0])
    elif signal==1:
        filenames = QFileDialog.getOpenFileName()
        if (filenames.isNull()):
            filenames = str(glob.glob('*.npy')[0])
    return filenames

And my "retranslate" browse button and lineEdit look like this:

self.lineEdit.setText(_translate("ROIGUI", str(self.fileSelect()), None))

self.Browse.clicked.connect(self.fileSelect)

My only problem is getting the text of my lineEdit to update; although the file in use itself updates after it's selected with Browse, the text itself doesn't update. Help?


回答1:


If the cancel button is selected from the QFileDialog filenames variable will be a null QString so, you could:

filenames = QFileDialog.getOpenFileName()
if (filenames.isNull()):
    self.lineEdit.setText(_translate("ROIGUI", filenames, None))
else:
    # The alternative code. Set the default value here to the QLineEdit.

Reference: QFileDialog.getOpenFileName()



来源:https://stackoverflow.com/questions/28595081/pyqt-file-browsing-setting-a-default-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!