Skip to content
Snippets Groups Projects
Commit 12dc6fc9 authored by Thomas Holene Løkkeborg's avatar Thomas Holene Løkkeborg
Browse files

#32 shortcuts fungerer nå ved flere TableModules i samme scene

Å legge til noen rader, for så å fjerne noen rader fungerer ikke, men dette er trolig ikke et problem med keyPressEvent

Under følger en snippet som viser hva som ikke fungerte før:
```python
from PySide2.QtWidgets import QVBoxLayout, QApplication, QWidget, QGraphicsScene, QGraphicsView, QTableWidget, QShortcut, QHBoxLayout
from PySide2.QtGui import QKeySequence
from PySide2.QtCore import Qt

class QTableWidthWithShortcuts(QTableWidget):
    def __init__(self, *args, **kwargs):
        super(QTableWidthWithShortcuts, self).__init__(*args, **kwargs)
        shortcut = QShortcut(QKeySequence("Shift++"), self, context=Qt.WidgetWithChildrenShortcut)
        shortcut.activated.connect(self.say_hello)

    def say_hello(self):
        print("Hello")

app = QApplication()

# inside QGrahpicsScene
# shortcuts don't work..
scene = QGraphicsScene()
view = QGraphicsView(scene)
t1 = QTableWidthWithShortcuts(2,2)
t1.move(0,0)
scene.addWidget(t1)
t2 = QTableWidthWithShortcuts(2,2)
t2.move(300,0)
scene.addWidget(t2)
view.show()

# inside QHBoxLayout
# shortcuts work!!
layout = QHBoxLayout()
t1 = QTableWidthWithShortcuts(2,2)
layout.addWidget(t1)
t2 = QTableWidthWithShortcuts(2,2)
layout.addWidget(t2)
wrapper_widget = QWidget()
wrapper_widget.setLayout(layout)
wrapper_widget.show()

app.exec_()
```

Her er en snippet som viser hvordan denne løsningen unngår problemet over:
```python
from PySide2.QtWidgets import QVBoxLayout, QApplication, QWidget, QGraphicsScene, QGraphicsView, QTableWidget, QShortcut, QHBoxLayout
from PySide2.QtGui import QKeySequence
from PySide2.QtCore import Qt

class QTableWidthWithShortcuts(QTableWidget):
    def __init__(self, uid, *args, **kwargs):
        super(QTableWidthWithShortcuts, self).__init__(*args, **kwargs)
        self.uid = uid

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Question:
            print("{}: Shift and plus!".format(self.uid))
        elif event.modifiers() == Qt.ShiftModifier and event.key() == Qt.Key_Underscore:
            print("{}: Shift and minus!".format(self.uid))
        elif event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_Plus:
            print("{}: Ctrl and plus!".format(self.uid))
        elif event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_Underscore:
            print("{}: Ctrl and minus!".format(self.uid))
        else:
            super(QTableWidthWithShortcuts, self).keyPressEvent(event)

app = QApplication()

scene = QGraphicsScene()
view = QGraphicsView(scene)
t1 = QTableWidthWithShortcuts("A", 2,2)
t1.move(0,0)
scene.addWidget(t1)
t2 = QTableWidthWithShortcuts("B", 2,2)
t2.move(300,0)
scene.addWidget(t2)
view.show()

app.exec_()
```
parent 8eddc2d4
No related branches found
No related tags found
1 merge request!21#32 shortcuts fungerer nå ved flere TableModules i samme scene
Pipeline #72781 failed
......@@ -58,7 +58,31 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
self.cellChanged.connect(self.resize)
self.set_shortcuts()
def keyPressEvent(self, event):
"""Launch actions when specific combinations of keys are pressed.
If the keys pressed are not related to a shortcut on this custom widget
the event is sent on to be handled by the superclass (for navigation
with arrow-keys for.eg.)
The combinations used below work on Thomas' machine, but are not
guaranteed to work on all machines. This needs to be checked.
Paramters
---------
event : QKeyEvent
event sent by Qt for us to handle
"""
if event.key() == QtCore.Qt.Key_Question:
self.add_column()
elif event.modifiers() == QtCore.Qt.ShiftModifier and event.key() == QtCore.Qt.Key_Underscore:
self.remove_column()
elif event.modifiers() == QtCore.Qt.ControlModifier and event.key() == QtCore.Qt.Key_Plus:
self.add_row()
elif event.modifiers() == QtCore.Qt.ControlModifier and event.key() == QtCore.Qt.Key_Underscore:
self.remove_row()
else:
super(TableModule, self).keyPressEvent(event)
def set_header_item(self, column, text):
"""Insert item with header-style.
......@@ -77,20 +101,6 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
item.setFont(HEADER_FONT)
self.setItem(0, column, item)
def set_shortcuts(self):
"""Set shortcuts for adding and removing rows and columns."""
# Create shortcuts
shortcut_add_col = QShortcut(QtGui.QKeySequence("Shift++"), self)
shortcut_rem_col = QShortcut(QtGui.QKeySequence("Shift+-"), self)
shortcut_add_row = QShortcut(QtGui.QKeySequence("Ctrl++"), self)
shortcut_rem_row = QShortcut(QtGui.QKeySequence("Ctrl+-"), self)
# Connect shortcuts to functions
shortcut_add_col.activated.connect(self.add_column)
shortcut_rem_col.activated.connect(self.remove_column)
shortcut_add_row.activated.connect(self.add_row)
shortcut_rem_row.activated.connect(self.remove_row)
def add_column(self):
"""Add column to the right of selected column."""
self.insertColumn(self.currentColumn() + 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment