예제 #1
0
class QTreeSelection(QToolButton):
    currentIndexChanged = pyqtSignal(QModelIndex, QModelIndex)
    currentDataChanged = pyqtSignal(object)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setPopupMode(QToolButton.MenuButtonPopup)
        self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

        self.tree = QTreeView(self)
        self.tree.setMinimumWidth(640)
        self.tree.setSelectionMode(QTreeView.SingleSelection)
        self.tree.setSelectionBehavior(QTreeView.SelectRows)

        act = QWidgetAction(self)
        act.setDefaultWidget(self.tree)

        self.menu = QMenu(self)
        self.menu.addAction(act)
        self.setMenu(self.menu)
        self.clicked.connect(self.showMenu)

    def _currentIndexChanged(self, newindex, oldindex):
        self.menu.close()
        selected = newindex.sibling(newindex.row(), 0)

        display = selected.data(Qt.DisplayRole)
        icon = selected.data(Qt.DecorationRole)

        self.setText(display or "")
        self.setIcon(icon or QIcon())
        self.currentIndexChanged.emit(newindex, oldindex)
        self.currentDataChanged.emit(newindex.data(Qt.UserRole))

    def currentIndex(self):
        return self.tree.currentIndex()

    def currentData(self):
        return self.currentIndex().data(Qt.UserRole)

    def setCurrentIndex(self, index):
        return self.tree.setCurrentIndex(index)

    def model(self):
        return self.tree.model()

    def setModel(self, model):
        self.tree.setModel(model)
        self.tree.selectionModel().currentChanged.connect(
            self._currentIndexChanged)

    def keyPressEvent(self, event):
        if (event.key() in (Qt.Key_Up, Qt.Key_Down)
                and not (event.modifiers()
                         & (Qt.ShiftModifier | Qt.AltModifier
                            | Qt.ControlModifier))):
            self.tree.keyPressEvent(event)

        return super().keyPressEvent(event)
예제 #2
0
    def keyPressEvent(self, keyEvent):
        """
        Function called by QT when a key has been pressed inside the treeView.
        Subclassed in order to call a callback function
        @param keyEvent: keyboard event
        print("\033[31;1mkeyPressEvent() key = {0}\033[m".format(keyEvent.key()))
        """

        if self.keyPressEventcallback and not self.signals_blocked:
            if not self.keyPressEventcallback(keyEvent):
                # key not accepted => send it back to the parent
                QTreeView.keyPressEvent(self, keyEvent)
        else:
            QTreeView.keyPressEvent(self, keyEvent)
예제 #3
0
    def keyPressEvent(self, keyEvent):
        """
        Function called by QT when a key has been pressed inside the treeView.
        Subclassed in order to call a callback function
        @param keyEvent: keyboard event
        print("\033[31;1mkeyPressEvent() key = {0}\033[m".format(keyEvent.key()))
        """

        if self.keyPressEventcallback and not self.signals_blocked:
            if not self.keyPressEventcallback(keyEvent):
                # key not accepted => send it back to the parent
                QTreeView.keyPressEvent(self, keyEvent)
        else:
            QTreeView.keyPressEvent(self, keyEvent)
예제 #4
0
    def keyPressEvent(self, event):

        if event.key() == Qt.Key_F2:
            self.f2Pressed.emit()
        else:
            return QTreeView.keyPressEvent(self, event)
예제 #5
0
 def keyPressEvent(self, event):
     QTreeView.keyPressEvent(self, event)
     item = self.selected_item
     if hasattr(item, 'key_pressed_event'): item.key_pressed_event(event)
     self.key_press_event(event)