gpt4 book ai didi

python - 从图像中选择区域,但可调整大小的 QRubberBand 不会调整大小

转载 作者:行者123 更新时间:2023-12-01 01:06:25 26 4
gpt4 key购买 nike

我想从图像中选择一个区域并获取她的坐标。我已经搜索过,找到了这段代码,它创建了对象橡皮筋,但我无法调整它的大小..奇怪的是,在 linux (lubuntu) 上它可以工作,但在 macOS 上不行..我想要 macOS 上的橡皮筋版本这些“握把”很特别,在 macOS 中,这些握把都不会出现..还有一件事,可以通过右键单击选择来移动橡皮筋矩形,然后在图像上移动到任何我想要的位置,就像将图标移动到桌面。谢谢!

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class ResizableRubberBand(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ResizableRubberBand, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.SubWindow)
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(
QtWidgets.QSizeGrip(self), 0,
QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
layout.addWidget(
QtWidgets.QSizeGrip(self), 0,
QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
self._band = QtWidgets.QRubberBand(
QtWidgets.QRubberBand.Rectangle, self)
self._band.show()
self.show()

def resizeEvent(self, event):
size = QtCore.QSize(3, 4)
size.scale(self.size(), QtCore.Qt.KeepAspectRatio)
self.resize(size)
self._band.resize(self.size())

class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Show Rubber Band')
self.button.clicked.connect(self.handleButton)
self.label = QtWidgets.QLabel()
self.label.setScaledContents(True)
self.label.setPixmap(QtGui.QPixmap('image.JPG'))
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)

def handleButton(self):
self.band = ResizableRubberBand(self.label)
self.band.setGeometry(50, 50, 150, 300)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 600, 500)
window.show()
sys.exit(app.exec_())

最佳答案

你走在正确的道路上。 PyQt 使用事件处理程序来控制鼠标交互和调整事件大小。对于您的应用程序,调整夹点大小将调用 resizeEvent 并重新绘制更改的窗口涉及 paintEvent。同样,鼠标交互利用 mousePressEventmouseMoveEventmouseReleaseEvent 作为触发这些操作时调用的事件处理程序。本质上,对于您想要的每个交互,您必须实现该特定事件的处理程序。我使用PyQt4进行测试,但PyQt5应该是相同的。

enter image description here

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# from PyQt4 import QtCore, QtWidgets

class ResizableRubberBand(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ResizableRubberBand, self).__init__(parent)

self.draggable = True
self.dragging_threshold = 5
self.mousePressPos = None
self.mouseMovePos = None
self.borderRadius = 5

self.setWindowFlags(QtCore.Qt.SubWindow)
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(
QtWidgets.QSizeGrip(self), 0,
QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
layout.addWidget(
QtWidgets.QSizeGrip(self), 0,
QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
self._band = QtWidgets.QRubberBand(
QtWidgets.QRubberBand.Rectangle, self)
self._band.show()
self.show()

def resizeEvent(self, event):
self._band.resize(self.size())

def paintEvent(self, event):
# Get current window size
window_size = self.size()
qp = QtWidgets.QPainter()
qp.begin(self)
qp.setRenderHint(QtWidgets.QPainter.Antialiasing, True)
qp.drawRoundedRect(0, 0, window_size.width(), window_size.height(),
self.borderRadius, self.borderRadius)
qp.end()

def mousePressEvent(self, event):
if self.draggable and event.button() == QtCore.Qt.RightButton:
self.mousePressPos = event.globalPos() # global
self.mouseMovePos = event.globalPos() - self.pos() # local
super(ResizableRubberBand, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.RightButton:
globalPos = event.globalPos()
moved = globalPos - self.mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# Move when user drag window more than dragging_threshold
diff = globalPos - self.mouseMovePos
self.move(diff)
self.mouseMovePos = globalPos - self.pos()
super(ResizableRubberBand, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if self.mousePressPos is not None:
if event.button() == QtCore.Qt.RightButton:
moved = event.globalPos() - self.mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# Do not call click event or so on
event.ignore()
self.mousePressPos = None
super(ResizableRubberBand, self).mouseReleaseEvent(event)

class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Show Rubber Band')
self.button.clicked.connect(self.handleButton)
self.label = QtWidgets.QLabel()
self.label.setScaledContents(True)
self.label.setPixmap(QtGui.QPixmap('image.JPG'))
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)

def handleButton(self):
self.band = ResizableRubberBand(self.label)
self.band.setGeometry(150, 150, 150, 150)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 600, 500)
window.show()
sys.exit(app.exec_())

关于python - 从图像中选择区域,但可调整大小的 QRubberBand 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55307811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com