Vlad
Vlad

Reputation: 419

How to in PySide/PyQt, zooming in and out UI

I am once in a while practicing PySide in Maya and right now I want to have UI which will act differently with different zoom range. Basically, when I zoom in on certain range I would like to see one type of buttons , when I zoom out , again different buttons. Anybody knows which layout I can look into or some examples? Thank you.

Upvotes: 0

Views: 1000

Answers (1)

TriSpade
TriSpade

Reputation: 71

For zooming I have a Custom QGraphicsView that looks like:

from PyQt5.QtWidgets import QGraphicsView, QSizePolicy


class GraphicsView(QGraphicsView):

    def __init__(self):
        super(GraphicsView, self).__init__()
        # Graphics view variables
        self.start = None
        self.end = None
        self.box_list = list()
        self.__zoom = 0
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

    def wheelEvent(self, event):
        if event.angleDelta().y() > 0:
            factor = 1.25
            self.__zoom += 1
        else:
            factor = 0.8
        if self.__zoom > 0:
            self.scale(factor, factor)
        elif self.__zoom == 0:
            # self.fitInView()
            pass
        else:
            self.__zoom = 0

Based on that you could have a pysignal and do an emit at certain self.__zoom levels allowing you to connect that signal to a method that would be responsible for changing a tool bar or whatever your needs are.

For Example:

from PyQt5.QtWidgets import QGraphicsView, QSizePolicy
from PyQt5.QtCore import pyqtSignal

class GraphicsView(QGraphicsView):

    zoom_signal = pyqtSignal(bool)

    def __init__(self):
        super(GraphicsView, self).__init__()
        # Graphics view variables
        self.start = None
        self.end = None
        self.box_list = list()
        self.__zoom = 0
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

    def wheelEvent(self, event):
        if event.angleDelta().y() > 0:
            factor = 1.25
            self.__zoom += 1
        else:
            factor = 0.8
        if self.__zoom > 0:
            self.scale(factor, factor)
        if self.__zoom > 10:
            self.zoom_signal.emit(True)
        elif self.__zoom < 10:
            self.zoom_signal.emit(False)
        else:
            self.__zoom = 0

This probably won't work as is for your needs but the concept is there and can be modified to do what you want.

Upvotes: 1

Related Questions