当函数参数是str时,QSignalMapper(在PyQt5中)不起作用

时间:2017-07-26 20:48:27

标签: python pyqt5

我尝试将我的代码从pyqt4迁移到pyqt5,但我在处理QSignalMapper时遇到了麻烦。

当我正在进行映射并且传递给函数的参数是一个int时,一切正常,但是当它是一个字符串时,没有任何事情发生。

以下代码不会返回任何错误,但只有#!/bin/sh # Copyright 2015 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. set -eu AUTH_DATA="$(curl -s -f -m 10 "http://metadata/computeMetadata/v1/instance/service-accounts/default/token" \ -H "Metadata-Flavor: Google")" R=$? if [ ${R} -ne 0 ]; then echo "curl for auth token exited with status ${R}" >&2 exit ${R} fi AUTH="$(echo "${AUTH_DATA}" \ | tr -d '{}' \ | sed 's/,/\n/g' \ | awk -F ':' '/access_token/ { print "_token:" $2 }' \ | tr -d '"\n' \ | base64 -w 0)" if [ -z "${AUTH}" ]; then echo "Auth token not found in AUTH_DATA ${AUTH_DATA}" >&2 exit 1 fi D="${HOME}/.docker" mkdir -p "${D}" cat > "${D}/config.json" <<EOF { "auths":{ "https://container.cloud.google.com":{"auth": "${AUTH}"}, "https://gcr.io":{"auth": "${AUTH}"}, "https://b.gcr.io":{"auth": "${AUTH}"}, "https://us.gcr.io":{"auth": "${AUTH}"}, "https://eu.gcr.io":{"auth": "${AUTH}"}, "https://asia.gcr.io":{"auth": "${AUTH}"}, "https://beta.gcr.io":{"auth": "${AUTH}"} } } EOF 行为正确,而不是act_int.trigger()

act_str.trigger()

仅打印:

from PyQt5 import QtGui,QtWidgets,QtCore

def a_function(x):
    print("x : ",x)
act_int = QtWidgets.QAction()
act_str = QtWidgets.QAction()

mapper = QtCore.QSignalMapper()
act_int.triggered.connect(mapper.map)
mapper.setMapping(act_int,1)
act_str.triggered.connect(mapper.map)
mapper.setMapping(act_str,"a_string")
mapper.mapped.connect(a_function)

act_int.trigger()
act_str.trigger()

正如我所料,它会打印出来:

x :  1

也许我没有正确使用QSignalMapper(我没有在网上找到pyqt5的例子)

1 个答案:

答案 0 :(得分:0)

您应该重载正确的方法:

mapped.mapped[str].connect(a_function)