PyQt 按钮选择 [英] PyQt Button Selection

查看:37
本文介绍了PyQt 按钮选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PyQt GUI 设置,其中包含一系列 QPushButton 和一个 QLineEdit 文本框(除其他外).设置文本框以便在 returnPressed() 时调用函数.我的问题是,当我单击文本框并输入文本时,其中一个按钮被选中,这意味着当我在文本框中按 Enter 键时,它会同时激活按钮和文本框功能.

I have a PyQt GUI set up that has a selection of QPushButtons and a QLineEdit text box (among other things). The text box is set up so as to call a function upon returnPressed(). My problem is that when I click on the text box and put in text, one of the buttons becomes selected which means that when I press enter in the text box it activates both the button and the text box function.

有没有办法解决这个问题?有什么方法可以在编辑文本框时阻止任何按钮被选中?

Is there a way around this? Some way to stop any buttons from being selected while the text box is being edited?

代码相当长,所以我无法在此处添加,但如果对布局或任何可能相关的内容有任何疑问,请提问.

The code is fairly long so I can't add it here but if there are any questions regarding layout or anything that may be relevant, please ask.

感谢您提供的任何帮助

推荐答案

根据您的问题和评论,我猜按钮和行编辑在 QDialog 中,并且选择/突出显示是由于 default/autoDefault 按钮的属性.

From your question and comments, I'm guessing that the buttons and line-edit are in a QDialog, and that the selection/highlighting occurs due to the default/autoDefault property of buttons.

通常,这些属性会设置为 False,但在 QDialog 中,它们会自动设置为 True.当前默认的按钮会在它周围绘制一个额外的框架(即使它没有键盘焦点),并且只要按下返回键就会被激活.

Normally, these properties will be set to False, but in a QDialog they are automatically set to True. The button that is the current default gets an additional frame drawn around it (even when it doesn't have the keyboard focus), and is activated whenever the return key is pressed.

您当然可以通过简单地执行以下操作来防止这种行为:

You can of course prevent this behaviour by simply doing:

button.setDefault(False)
button.setAutoDefault(False)

对于对话框中的每个按钮.

for each button in the dialog.

这篇关于PyQt 按钮选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆