关键词:python qt4 信号和槽 SignalMapper
还是举例说明比较好写:
1)C++版本:
QSignalMapper *signalMapper = new QSignalMapper(this);
for (i = 0; i < 10; i++) {
button = new QPushButton(QObject::tr(buttonName));
signalMapper->setMapping(button[i], i);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(handle(int)));
2)pyqt4版本:
|
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *class Window(QWidget):def __init__(self, parent = None):QWidget.__init__(self, parent)layout = QVBoxLayout(self)mapper = QSignalMapper(self)for i in range(5):button = QPushButton()button.setText("Button " + str(i))self.connect(button, SIGNAL("clicked()"), mapper, SLOT("map()"))if i % 2 == 0:mapper.setMapping(button, str(i))else:mapper.setMapping(button, i)layout.addWidget(button)self.connect(mapper, SIGNAL("mapped(const QString &)"), self.stringMapped)self.connect(mapper, SIGNAL("mapped(int)"), self.intMapped)def stringMapped(self, value):print "stringMapped", valuedef intMapped(self, value):print "intMapped", valueif __name__ == "__main__":app = QApplication(sys.argv)window = Window()window.show()sys.exit(app.exec_()) |