Focus-moving on board works

This commit is contained in:
Sebastian Lohff 2013-11-18 18:33:43 +01:00
parent 4376d4e6e3
commit 1bae9069a0
1 changed files with 29 additions and 0 deletions

29
game.py
View File

@ -42,6 +42,7 @@ class SeopardyGame(QtGui.QWidget):
b.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) b.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
b.setAutoDefault(False) b.setAutoDefault(False)
b.setStyleSheet("QPushButton { font-size: 60px; }") b.setStyleSheet("QPushButton { font-size: 60px; }")
b.installEventFilter(FocusKeyGrabber(i+1, j+1, self))
b.clicked.connect(lambda sec=sec, j=j: self.go_to_question(sec, j+1)) b.clicked.connect(lambda sec=sec, j=j: self.go_to_question(sec, j+1))
board.addWidget(b, j+1, i) board.addWidget(b, j+1, i)
layout.addLayout(board) layout.addLayout(board)
@ -57,6 +58,11 @@ class SeopardyGame(QtGui.QWidget):
self.setLayout(layout) self.setLayout(layout)
def setFocusToQuestion(self, secno, number):
if secno < 1 or secno > len(self.questions.get_sections()) or number > 5 or number < 1:
return
self.board.itemAtPosition(number, secno-1).widget().setFocus()
def event(self, e): def event(self, e):
if e.type() == QtCore.QEvent.KeyPress and e.key() == QtCore.Qt.Key_Left: if e.type() == QtCore.QEvent.KeyPress and e.key() == QtCore.Qt.Key_Left:
@ -169,3 +175,26 @@ class SeopardyGame(QtGui.QWidget):
btn.setStyleSheet("QPushButton { background-color: %s; color: white; font-size: 20px; border: none; }" % (btncolor.name(),)) btn.setStyleSheet("QPushButton { background-color: %s; color: white; font-size: 20px; border: none; }" % (btncolor.name(),))
btn.setText(btnstr.strip()) btn.setText(btnstr.strip())
class FocusKeyGrabber(QtCore.QObject):
def __init__(self, secno, number, parent, *args, **kwargs):
super(FocusKeyGrabber, self).__init__(parent, *args, **kwargs)
self._secno = secno
self._number = number
self._parent = parent
def eventFilter(self, obj, e):
if e.type() == QtCore.QEvent.KeyPress:
if e.key() == QtCore.Qt.Key.Key_Left:
self._parent.setFocusToQuestion(self._secno-1, self._number)
return True
elif e.key() == QtCore.Qt.Key.Key_Right:
self._parent.setFocusToQuestion(self._secno+1, self._number)
return True
elif e.key() == QtCore.Qt.Key.Key_Up:
self._parent.setFocusToQuestion(self._secno, self._number-1)
return True
elif e.key() == QtCore.Qt.Key.Key_Down:
self._parent.setFocusToQuestion(self._secno, self._number+1)
return True
return QtCore.QObject.eventFilter(self, obj, e)