zoom functionality problems

60 Views Asked by At

i am creating a text editor (as so many do) and right now i am trying to implement a zoom function, only to run into a problem. it is a bit difficult to explain but basiclly when zooming in the Qtextbox ends up going under an overlaid box, what i believe to be the QGraphicsScene functions doing. i couldnt find a way to work around this function, and have no way of confirming that it is the problem.

for clarity i want the page zoom in to act like any other word or google doc, making the text box bigger (I'll set a maximum later, the page size is up for change)

heres the important snippet of code

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *


class ToolBar(QToolBar):
    def __init__(self, central_widget):
        super().__init__()
        self.central_widget = central_widget
        self.create_toolbar()

    def create_toolbar(self):
        zoom_in_button = QPushButton("Zoom In", self)
        zoom_in_button.clicked.connect(self.central_widget.zoom_in)

        zoom_out_button = QPushButton("Zoom Out", self)
        zoom_out_button.clicked.connect(self.central_widget.zoom_out)

        reset_zoom_button = QPushButton("Reset Zoom", self)
        reset_zoom_button.clicked.connect(self.central_widget.reset_zoom)

        self.addWidget(zoom_in_button)
        self.addWidget(zoom_out_button)
        self.addWidget(reset_zoom_button)


class ZoomableTextBox(QGraphicsView):
    def __init__(self, parent=None):
        super(ZoomableTextBox, self).__init__(parent)

        self.scene = QGraphicsScene(self)
        self.text_edit = QTextEdit()
        self.text_edit.setFixedSize(500, 800) #placeholder size
        self.scene.addWidget(self.text_edit)

        self.setScene(self.scene)

        self.zoom_factor = 1.0

    def zoom_in(self):
        self.zoom_factor *= 1.2
        self.scale(1.2, 1.2)
        self.update_scene_rect()

    def zoom_out(self):
        self.zoom_factor /= 1.2
        self.scale(1 / 1.2, 1 / 1.2)
        self.update_scene_rect()

    def reset_zoom(self):
        self.zoom_factor = 1.0
        self.resetTransform()
        self.update_scene_rect()

    def update_scene_rect(self):
        # Update the scene rectangle to ensure it fits the visible items
        items_rect = self.scene.itemsBoundingRect()
        self.setSceneRect(items_rect)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.text_box = ZoomableTextBox()  # Set self.text_box here
        self.tool_bar = ToolBar(self.text_box)  # Pass self.text_box to the toolbar

        # Add the toolbar to the main window
        self.addToolBar(self.tool_bar)

        # Set the central widget as the text box
        central_widget = QWidget()
        layout = QVBoxLayout(central_widget)
        layout.addStretch(1)
        layout.addWidget(self.text_box, alignment=Qt.AlignCenter)
        layout.addStretch(1)

        self.setCentralWidget(central_widget)

        # Set window properties
        self.setWindowTitle('placeholder')
        self.setGeometry(100, 100, 800, 600)

        # Show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

0

There are 0 best solutions below