Search code examples
python-3.xpyqt6

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


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


Solution

  • 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