Reputation: 356
I have made a UI in QT designer and converted it with PySide. I am now trying to attach some code to one of the buttons in the design. I am trying to create a file dialog when the toolbutton is pressed, but I am having issues with the file dialog opening before the main window and then the toolbutton not doing anything. This is the code I have, minus some formatting lines:
from PySide import QtCore, QtGui
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.horizontalLayoutWidget = QtGui.QWidget(MainWindow)
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.Filepathselector_2 = QtGui.QLineEdit(self.horizontalLayoutWidget)
self.horizontalLayout.addWidget(self.Filepathselector_2)
self.toolButton_2 = QtGui.QToolButton(self.horizontalLayoutWidget)
self.horizontalLayout.addWidget(self.toolButton_2)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), self.Filepathselector_2.clear)
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), showDialog(self))
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.Filepathselector_2.setToolTip(QtGui.QApplication.translate("MainWindow", "Select file", None, QtGui.QApplication.UnicodeUTF8))
self.toolButton_2.setText(QtGui.QApplication.translate("MainWindow", "...", None, QtGui.QApplication.UnicodeUTF8))
def showDialog(self):
fname, _ = QtGui.QFileDialog.getOpenFileName(MainWindow, 'Open file', "", 'MP3 Files (*.mp3)')
print fname
print _
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I am using two variables for the file selection as PySide has a bug with returning a tuple instead of a string here. The code runs the GUI fine, but opens the file dialog before the GUI is loaded, and then when I try to use the toolbutton nothing happens. Where am I going wrong with this?
Upvotes: 1
Views: 1802
Reputation: 8999
You're executing the function call in your connect statement with showDialog(self)
, so change the line
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), showDialog(self))
to
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), self.showDialog)
and indent the function showDialog
so that it is inside your class. This means you don't have to pass it the self
parameter at all.
That fixed it for me.
Upvotes: 2
Reputation: 219
The problem is on this line:
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), showDialog(self))
Specifically, showDialog(self)
is calling showDialog
and passing its return value as the slot to connect. In this case you could make showDialog
a method, so self
is passed implicitly, and connect it like this:
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), self.showDialog)
You could also use a lambda, like so:
QtCore.QObject.connect(self.toolButton_2, QtCore.SIGNAL("clicked()"), lambda: showDialog(self))
The lambda method is more generally applicable (e.g. you could pass something other than self
for the argument).
Upvotes: 2