[英]Refer to a signal whose name is passed as string (new-style signals/slots)?
[英]Using new style signal and slots for combobox?
我有兩行使用舊的 SIGNAL 和 SLOT 樣式..
combobox.emit(SIGNAL("activated(int)"), combobox.currentIndex())
combobox.emit(SIGNAL("activated(const QString &)"), combobox.currentText())
我想知道新風格會是什么樣子。 我是 python 新手,對信號和插槽沒有太多經驗。 是否有一個很好的資源可以涵蓋這一點? 文檔並沒有真正幫助我理解發生了什么。
解決方案是指示正在發出的信號的參數類型:
combo.activated[type].connect(someSlot)
例子:
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QVBoxLayout())
combo = QComboBox(self)
self.layout().addWidget(combo)
combo.addItems(["item1", "item2", "item3"])
combo.activated[int].connect(self.onActivatedIndex)
combo.activated[str].connect(self.onActivatedText)
@pyqtSlot(int)
def onActivatedIndex(self, index):
print(index)
@pyqtSlot(str)
def onActivatedText(self, text):
print(text)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.