I want to store a selected file’s location as a string in Python. I am trying to use QFileDialog to accomplish this, I have:
self.filedialog = QtGui.QFileDialog(self)
self.filedialog.show()
filepath = str(self.filedialog.getOpenFileName())
This opens two QFileDialog windows. Interestingly, one of the windows does not inherit the ‘style’ of my GUI, set my setStyle, but does return the filepath string. The other QFileDialog does inherit the style, but can not return the filepath string. I have found the QFileDialog documentation helpful, but have not been able to create a QFileDialog box that both produces the filepath string, and inherits the style of my GUI. What mistake am I making?
You actually created 2 windows.
The function
QFileDialog.getOpenFileNameis static, meaning that it creates its ownQFileDialogobject, shows the window, waits for the user to select a file and returns the choosen filename.You should only need that line:
If you set the style at the application level (with
QApplication.setStyle), it might be applied to the window if you use the non-native dialog: