简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM