A button defined as default with the qt designer GUI does not respond to return on Linux/Mac.

huangapple go评论70阅读模式
英文:

A button defined as default with the qt designer GUI does not respond to return on Linux/Mac

问题

我正在学习PyQt6。使用Qt Designer创建了一个简单的GUI。请看截图。代码也可以在那里看到。

我想要实现的目标是,我的GUI中的按钮是默认按钮,因此在输入数字后按Enter/Return键会触发转换。

在macOS和Linux下都没有成功。

以下是您的代码部分:

from PyQt6.QtWidgets import QApplication, QWidget
import sys
from PyQt6 import uic
from rechner import Rechner

class UI(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("maingui.ui", self)
        self.button.setAutoDefault(True)
        self.button.clicked.connect(self.handle_button_click)   # 将按钮连接到一个函数

    def handle_button_click(self):
        rechner = Rechner()
        kelvin, fahrenheit = rechner.umrechnung(int(self.eingabe.text()))
        self.label_kelvin.setText(str(kelvin) + " K")
        self.label_fahrenheit.setText(str(fahrenheit) + " F")

app = QApplication(sys.argv)
window = UI()
window.show()
app.exec()

我已经尝试过autoDefaultdefault,以及同时使用两者。另外,我在代码中添加了一行self.button.setAutoDefault(True)。不幸的是,按钮未明显显示为默认按钮,并且不像通常情况下那样响应Enter/Return键。

这里是Qt Designer的截图和代码

英文:

I am just working my way to PyQt6. Created a simple GUI using Qt Designer. See screenshot. The code can be seen there as well.

What I want to achieve is that the button in my GUI is a default button and thus after entering a number by Enter/Return the conversion is triggered.

Neither under macOS nor under Linux

Hier noch mein Code:

from PyQt6.QtWidgets import QApplication, QWidget
import sys
from PyQt6 import uic
from rechner import Rechner

class UI(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("maingui.ui", self)
        self.button.setAutoDefault(True)
        self.button.clicked.connect(self.handle_button_click)   # connect the button to a function

    def handle_button_click(self):
        rechner = Rechner()
        kelvin, fahrenheit = rechner.umrechnung(int(self.eingabe.text()))
        self.label_kelvin.setText(str(kelvin) + " K")
        self.label_fahrenheit.setText(str(fahrenheit) + " F")

app = QApplication(sys.argv)
window = UI()
window.show()
app.exec()

I have already tested autoDefault as well as default and both at the same time. Also in the code I added a line self.button.setAutoDefault(True). Unfortunately, the button is not displayed recognizably as a default button and does not respond to Enter/Return as usual.

here a screenshot from qt designer and the code

答案1

得分: 0

谢谢您提供的解决方案。

> 如果您更仔细地阅读文档(特别是默认属性部分),您会发现它明确提到了对话框。您只是在使用一个QWidget,它不支持这样的机制。您需要使用QDialog。 - musicamante

英文:

Thank you for the solution.

> If you read the documentation more carefully (especially the default property), you'll see that it specifically refers to dialogs. You're just using a QWidget, which doesn't support such a mechanism. You need to use a QDialog. – musicamante

huangapple
  • 本文由 发表于 2023年8月4日 21:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定