i need a string from the main window to be displayed on a dialog and im having some issues...
heres my code:
class Ui_MainWindow(QtGui.QMainWindow):
drive_signal = QtCore.pyqtSignal(str)
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(459, 280)
..... #non relevant code
.....
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1][1:]
self.drive_combo.clear()
self.drive_combo.addItems(drives)
self.drive_signal.emit(self.drive_combo.currentText())
.....
.....
class SubDialog(QtGui.QDialog):
def setupUi(self, Dialog):
Dialog.setWindowTitle(Ui_MainWindow.drive_signal.connect())
Dialog.resize(532, 285)
.....
.....
but i get this error:
AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'
any help?
There are two errors on this line:
Dialog.setWindowTitle(Ui_MainWindow.drive_signal.connect())
Firstly, the two method calls are round the wrong way, and secondly, you are attempting to connect an unbound signal
to a slot, which can't work.
For the code to work, it would need to look something like this:
window.drive_signal.connect(dialog.setWindowTitle)
where window
is an instance of Ui_MainWindow
, and dialog
is an instance of SubDialog
.
Given the way the rest of the code is written, there may also be potential issues with the way the classes are initialized.
The code below shows one way to get the signal working correctly. Take careful note of the order in which things are done before starting the event loop:
from PyQt4 import QtGui, QtCore
class Ui_MainWindow(QtGui.QMainWindow):
drive_signal = QtCore.pyqtSignal(str)
def setupUi(self, MainWindow):
MainWindow.resize(459, 280)
MainWindow.setWindowTitle('MainWindow: Foo')
self.drive_signal.emit('Dialog: Bar')
class SubDialog(QtGui.QDialog):
def setupUi(self, Dialog):
Dialog.resize(532, 285)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Ui_MainWindow()
dialog = SubDialog()
window.drive_signal.connect(dialog.setWindowTitle)
window.setupUi(window)
dialog.setupUi(dialog)
window.show()
dialog.show()
sys.exit(app.exec_())