Created
May 13, 2022 07:47
-
-
Save mosolovsa/1bcbc2a87dc27869618266bea62a4d76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide2.QtCore import QTimer, QObject, SIGNAL | |
from PySide2.QtGui import QResizeEvent | |
from PySide2.QtWidgets import QApplication, QWidget, QLabel, QMainWindow | |
from PySide2.QtSvg import QSvgWidget | |
import sys | |
class AppWindow(QMainWindow): | |
def __init__(self, parent=None): | |
super(AppWindow, self).__init__(parent) | |
self.central = QSvgWidget("test_svg.svg") | |
self.foo = QLabel("foo", parent=self.central) | |
self.setCentralWidget(self.central) | |
self.align_label_to_rectangle() | |
def resizeEvent(self, event: QResizeEvent) -> None: | |
super(AppWindow, self).resizeEvent(event) | |
print('need recalculation') | |
self.align_label_to_rectangle() | |
def align_label_to_rectangle(self): | |
print('viewBoxF', self.central.renderer().viewBoxF()) | |
print('x, y', self.central.x(), self.central.y()) | |
print('size', self.central.size()) | |
bound1 = self.central.renderer().boundsOnElement("foo") | |
print(bound1) | |
transform = self.central.renderer().transformForElement("foo") | |
print(transform) | |
bound2 = transform.mapRect(bound1) | |
print(bound2) | |
tl = bound2.topLeft() | |
x_linear_k = self.central.width() / self.central.renderer().viewBoxF().width() | |
y_linear_k = self.central.height() / self.central.renderer().viewBoxF().height() | |
print(tl) | |
self.foo.move(x_linear_k * tl.x(), y_linear_k * tl.y()) | |
app = QApplication(sys.argv) | |
window = AppWindow() | |
window.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment