Qt Python 组合框“currentIndexChanged”发射两次
我有一个组合,表现出一些尴尬的行为。给定组合框中的选项列表,用户应该用鼠标单击选择城市的名称。代码如下:
QtCore.QObject.connect(self.comboCity, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCity)
def checkChosenCity(self):
self.cityName=self.comboCity.currentText()
print "the city chosen is:"
print "%s" % self.cityName
问题是,每次选择一个城市时,connect
都会调用函数 checkChosenCity
两次。
该组合是分层组合,即在第一个组合中选择客户之后,第二个组合框中出现该客户的城市列表。
我希望这里有人能指出或猜测为什么会发生这种情况。
I have a combo, which is showing some awkward behavior. Given a list of options from the combo-box, the user should pick the name of a city clicking with the mouse. Here is the code:
QtCore.QObject.connect(self.comboCity, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCity)
def checkChosenCity(self):
self.cityName=self.comboCity.currentText()
print "the city chosen is:"
print "%s" % self.cityName
The problem is, each time a city is chosen, connect
calls the function checkChosenCity
twice.
This combo is a hierarchical combo, i.e. after in the first combo a customer is chosen, then in the second combo-box comes the list of cities for that customer.
I hope someone here can point out or guess why this is happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有完全相同的问题。经过一些调试后发现,使用
currentIndexChanged(int)
而不是
currentIndexChanged(QString)
为我解决了这个问题。
它仍然不明白为什么前者会发射两次。
I had exactly the same problem. After some debugging it turned out that using
currentIndexChanged(int)
instead of
currentIndexChanged(QString)
fixed it for me.
It still don't understand why the former fires twice.
谢谢 Eli..
这就是我所得到的:
我发现,即使没有选择城市,城市的组合框也会被激活。是的,我已经检查过函数“checkChosenCity”是否未在程序内的其他任何地方调用。
作为一个快速修复,而不是理想的解决方案,我在函数“checkChosenCity”中添加了一个避免问题的条件。因此,现在当此功能被“连接”错误激活时,它会检查是否确实选择了城市名称,如果没有,则指向的进程不会运行,以避免系统崩溃。
这是将城市列表加载到组合框的函数:
这是将客户名称列表加载到组合框的函数:
这是检查是否选择了客户名称的函数:
这是检查的函数如果从列表中选择某个城市到组合框:
这是连接组合框事件的主要功能:
确实不是理想的解决方案。但是,非常有趣的是,必须花费几个小时才能发现如此奇怪的连接事件被错误地自我激活。
如果您发现任何其他解释,请告诉我们。
Thanks Eli..
Here is what I have:
What I find out is that, even when no city is chosen, the combox-box for city is activated. And yes, I have checked if the function 'checkChosenCity' is not called anywhere else inside the program.
As a quick fix, not the ideal solution, I put a condition to avoid the problem into the function 'checkChosenCity'. So, now when this function is wrongly activated by 'connect', it checks if a city name was really selected, if none, then the pointed process doesn't run avoiding the system to crash.
Here is the function that loads the city list into combo-box:
Here is the function that loads the customer-names list into combo-box:
Here is the function that checkes if the a customer name was chosen:
Here is the function that checks if some city chosen from list into combo-box:
Here is the main function that connect combo-box events:
Not the ideal solution really. But, quite funny to have to spend hours to find out that such a strange connect event is being wrongly self-activated.
If you discover any other explanation, just let us know.