In Qt Designer 5, how do I create a signal to open a QFileDialog
? I'm using Python and PyQt. I've tried creating signals with "Edit Signals/Slots" and I can select the button I want as sender, but I can't select an arbitrary function as the receiver, only existing widgets are available in the list.
Asked
Active
Viewed 1.5k times
6

jgorostegui
- 1,290
- 1
- 8
- 13

Kolargol00
- 1,697
- 2
- 17
- 21
1 Answers
9
In order to create custom Signal/Slots to later use in your Python application you need to add them doing a right click on the widget and clicking on Change signals/slots..., as shown in the next image:
You'll need to add the desired slots, like shown here with the mybutton_clicked()
function:
Thus far, the slots is created and it is possible to use it in the Signals & Slots Editor tab. Once in this tab, clicking in +
button, the Receiver slot is present if the previous step is done right, as shown here:
Lastly, integrate the requested QFileDialog
into the button press method, it is as easy as this :
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt5 import uic
import sys
form_class = uic.loadUiType("mainWindow.ui")[0] # Load the UI
class MyWindowClass(QMainWindow, form_class):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
def mybutton_clicked(self):
options = QFileDialog.Options()
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*)", options=options)
if fileName:
print(fileName)
app = QApplication(sys.argv)
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()

jgorostegui
- 1,290
- 1
- 8
- 13
-
2Or you can bypass all the designer rigmarole and just add the line `self.mybutton.clicked.connect(self.mybutton_clicked)` to the end of `__init__()`. – ekhumoro Feb 06 '17 at 21:42
-
I totally agree. I suppose that the Qt designer is somehow required to do this task. – jgorostegui Feb 06 '17 at 22:24
-
I need to add the `mybutton_clicked()` function by clicking on the green `+` in the "Change signals/slots..." dialog, right? – Kolargol00 Feb 08 '17 at 19:03
-
Yes. In the Slots box. – jgorostegui Feb 08 '17 at 19:06
-
And this is only available in Qt Creator, not Qt Designer? – Kolargol00 Feb 08 '17 at 19:08
-
Since Qt5 Qt Designer is part of Qt Creator. – jgorostegui Feb 08 '17 at 19:14
-
For anyone here, the suggestion by @ekhumoro is the pythonic, Qtic way to do this. Indeed, I would encourage ek to turn this into an answer (sometimes the right answer is to not actually answer the question being asked directly, but to reframe the question and show the OP the correct way, free of code smells). For the "good" way, see this Q&A: https://stackoverflow.com/questions/3196353/pyqt4-file-select-widget – eric Jan 05 '22 at 05:01
-
1@eric TBF, the answer makes this approach seem much more complicated than it actually needs to be. On the very rare occasions I use it, I just click the Edit Signals/Slots toolbar button, drag a connection, and then add the slot via the dialog that automatically pops up. The end result is effectively the same line of code, only added to `setupUi` instead of `__init__`. So you could say it's really more a question of convenience than of creating idiomatic code. – ekhumoro Jan 05 '22 at 14:57