Вот код, который у меня есть (я использую PyQt5, но я почти уверен, что C++ Qt будет работать так же)< /p>
Код: Выделить всё
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
width, height = 2000, 2000
grid_step = 100
class MyWidget(QLabel):
def __init__(self):
super().__init__()
self.setStyleSheet("background-color: black")
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
print(f"Position: {event.pos()}")
class ImageApp(QMainWindow):
def __init__(self, image_path=None):
super().__init__()
self.label = MyWidget()
self.label.setAlignment(Qt.AlignCenter)
self.scroll_area = QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setWidget(self.label)
self.setCentralWidget(self.scroll_area)
# Generate grid pixmap
self.pixmap = QPixmap(width, height)
self.pixmap.fill(Qt.black)
painter = QPainter(self.pixmap)
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine))
for x in range(0, width, grid_step):
painter.drawLine(x, 0, x, height)
for y in range(0, height, grid_step):
painter.drawLine(0, y, width, y)
painter.end()
self.resize(1000, 800)
def resizeEvent(self, event):
super().resizeEvent(event)
scaled_pixmap = self.pixmap.scaled(self.size().shrunkBy(QMargins(1, 1, 1, 1)),
Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.label.setPixmap(scaled_pixmap)
self.label.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ImageApp()
window.show()
sys.exit(app.exec_())
Итак, вопрос: существует ли быстрый и удобный, возможно, встроенный в Qt способ пересчета между окном и координаты изображения?
Подробнее здесь: https://stackoverflow.com/questions/791 ... s-on-label