0

我有一个看起来像这样的 Qt 小部件:

class launchiiwidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtWidgets.QVBoxLayout(self)

        self.textbox = QtWidgets.QTextEdit(self)
        self.textbox.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
        self.textbox.setAlignment(QtCore.Qt.AlignCenter)   
        self.textbox.setFixedSize(QtCore.QSize(600, 100))
        self.textbox.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.textbox.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        
        layout.addWidget(self.textbox)

        font = self.textbox.font()
        font.setPointSize(80)
        self.textbox.setFont(font)

        self.listwidget = QtWidgets.QListWidget(self)
        self.listwidget.addItem("Red")
        self.listwidget.addItem("Blue") 
        layout.addWidget(self.listwidget)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = launchiiwidget()
    widget.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    widget.resize(600, 200)
    widget.show()

    sys.exit(app.exec())

我怎样才能做到这一点,当按下“返回”或“右箭头键”时,焦点从当前位置移动到第一个项目listwidget?这也应该在专注于文本框内时起作用,而不会触发换行符。

注意:项目被动态添加到列表小部件。

4

1 回答 1

2

一个可能的解决方案可能是使用 QShorcut 但因为 OP 需要“不触发换行符”。所以在这种情况下,解决方案是为 QWindow 实现一个事件过滤器:

import sys

from PyQt6 import QtCore, QtGui, QtWidgets


class KeyHelper(QtCore.QObject):
    pressed = QtCore.pyqtSignal()

    def __init__(self, window):
        super().__init__(window)
        self._window = window

        self.window.installEventFilter(self)

    @property
    def window(self):
        return self._window

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.Type.KeyPress:
            if event.key() in (
                QtCore.Qt.Key.Key_Return,
                QtCore.Qt.Key.Key_Enter,
                QtCore.Qt.Key.Key_Right,
            ):
                self.pressed.emit()
                return True
        return super().eventFilter(obj, event)


class Launchiiwidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.textbox = QtWidgets.QTextEdit()
        self.textbox.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.NoWrap)
        self.textbox.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        self.textbox.setFixedSize(QtCore.QSize(600, 100))
        self.textbox.setVerticalScrollBarPolicy(
            QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
        )
        self.textbox.setHorizontalScrollBarPolicy(
            QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
        )

        font = self.textbox.font()
        font.setPointSize(80)
        self.textbox.setFont(font)

        self.listwidget = QtWidgets.QListWidget()
        self.listwidget.addItem("Red")
        self.listwidget.addItem("Blue") 

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.textbox)
        layout.addWidget(self.listwidget)

    def update_focus(self):
        self.listwidget.setFocus()
        index = self.listwidget.model().index(0, 0)
        if index.isValid():
            self.listwidget.setCurrentIndex(index)


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = Launchiiwidget()
    widget.setWindowFlags(QtCore.Qt.WindowType.FramelessWindowHint)
    widget.resize(600, 200)
    widget.show()

    key_helper = KeyHelper(widget.windowHandle())
    key_helper.pressed.connect(widget.update_focus)

    sys.exit(app.exec())
于 2021-08-07T19:39:13.507 回答