目前,我使用PySide在Ubuntu 14.04 LTS下进行图像裁剪。我在answer中遇到了post,它描述了如何进行图像裁剪的粗略概念。现在我想改变裁剪矩形的宽度和颜色。在this之后,我理解我应该创建一个子类QRubberBand
并覆盖paintEvent
方法。所以我在底部得到了python
代码。如您所见,我在pen.setWidth(5)
中进行了paintEvent
。但是,当我进行图像裁剪时,线宽不会改变(默认情况下似乎保持1
)。请参阅以下红色矩形。
请帮忙。谢谢!
import sys
from PyQt4 import QtGui, QtCore
class MyRubberBand(QtGui.QRubberBand):
def __init__(self, QRubberBand_Shape, QWidget_parent=None):
super(MyRubberBand, self).__init__(QRubberBand_Shape, QWidget_parent)
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.WindowText, QtGui.QBrush(QtCore.Qt.red))
self.setPalette(palette)
def paintEvent(self, QPaintEvent):
painter = QtGui.QStylePainter(self)
# painter.begin(self)
pen = QtGui.QPen()
# pen.setStyle(QtCore.Qt.SolidLine)
# pen.setColor(QtGui.QColor(QtCore.Qt.red))
pen.setWidth(5)
painter.setPen(pen)
option = QtGui.QStyleOptionFocusRect()
option.initFrom(self)
painter.drawControl(QtGui.QStyle.CE_FocusFrame, option)
class QExampleLabel (QtGui.QLabel):
def __init__(self, parentQWidget = None):
super(QExampleLabel, self).__init__(parentQWidget)
self.initUI()
def initUI (self):
self.setPixmap(QtGui.QPixmap('images/3.png'))
self.currentQRubberBand = MyRubberBand(QtGui.QRubberBand.Rectangle, self)
def mousePressEvent (self, eventQMouseEvent):
print 'mouse pressed'
self.originQPoint = eventQMouseEvent.pos()
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, QtCore.QSize()))
self.currentQRubberBand.show()
def mouseMoveEvent (self, eventQMouseEvent):
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())
def mouseReleaseEvent (self, eventQMouseEvent):
print 'mouse released'
# self.currentQRubberBand.hide()
currentQRect = self.currentQRubberBand.geometry()
# self.currentQRubberBand.deleteLater()
cropQPixmap = self.pixmap().copy(currentQRect)
cropQPixmap.save('output.png')
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQExampleLabel = QExampleLabel()
myQExampleLabel.show()
sys.exit(myQApplication.exec_())
答案 0 :(得分:0)
我知道这个问题很旧,但是最近我试图做同样的事情并且看到了这个问题,所以我想为其他可能遇到此问题的人发布一些代码。
您的代码已更新paintEvent
。
PySide2:
import sys
from PySide2.QtCore import Qt, QRect, QSize
from PySide2.QtGui import QColor, QPen, QBrush, QPixmap, QPainter, QPalette
from PySide2.QtWidgets import QApplication, QLabel, QRubberBand
class MyRubberBand(QRubberBand):
def __init__(self, QRubberBand_Shape, QWidget_parent=None):
super(MyRubberBand, self).__init__(QRubberBand_Shape, QWidget_parent)
palette = QPalette()
palette.setBrush(QPalette.WindowText, QBrush(Qt.red))
self.setPalette(palette)
def paintEvent(self, QPaintEvent):
###QRubberBand with black border and transparent background###
painter = QPainter(self)
###Border Color with width set at 6###
pen_color = QColor(0, 0, 0, 255)
painter.setPen(QPen(pen_color, 6))
###Transparent background###
brush_color = QColor(Qt.transparent)
###Or to use solid background color###
# brush_color = QColor(Qt.red)
painter.setBrush(QBrush(brush_color))
painter.drawRect(QPaintEvent.rect())
class QExampleLabel (QLabel):
def __init__(self, parentQWidget = None):
super(QExampleLabel, self).__init__(parentQWidget)
self.initUI()
def initUI (self):
self.setPixmap(QPixmap('images/3.png'))
self.currentQRubberBand = MyRubberBand(QRubberBand.Rectangle, self)
def mousePressEvent (self, eventQMouseEvent):
print('mouse pressed')
self.originQPoint = eventQMouseEvent.pos()
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, QSize()))
self.currentQRubberBand.show()
def mouseMoveEvent (self, eventQMouseEvent):
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())
def mouseReleaseEvent (self, eventQMouseEvent):
print('mouse released')
# self.currentQRubberBand.hide()
currentQRect = self.currentQRubberBand.geometry()
# self.currentQRubberBand.deleteLater()
cropQPixmap = self.pixmap().copy(currentQRect)
cropQPixmap.save('output.png')
if __name__ == '__main__':
myQApplication = QApplication(sys.argv)
myQExampleLabel = QExampleLabel()
myQExampleLabel.resize(800, 600)
myQExampleLabel.show()
sys.exit(myQApplication.exec_())
PyQt5:
import sys
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtGui import QColor, QPen, QBrush, QPixmap, QPainter, QPalette
from PyQt5.QtWidgets import QApplication, QLabel, QRubberBand
class MyRubberBand(QRubberBand):
def __init__(self, QRubberBand_Shape, QWidget_parent=None):
super(MyRubberBand, self).__init__(QRubberBand_Shape, QWidget_parent)
palette = QPalette()
palette.setBrush(QPalette.WindowText, QBrush(Qt.red))
self.setPalette(palette)
def paintEvent(self, QPaintEvent):
###QRubberBand with black border and transparent background###
painter = QPainter(self)
###Border Color with width set at 6###
pen_color = QColor(0, 0, 0, 255)
painter.setPen(QPen(pen_color, 6))
###Transparent background###
brush_color = QColor(Qt.transparent)
###Or to use solid background color###
# brush_color = QColor(Qt.red)
painter.setBrush(QBrush(brush_color))
painter.drawRect(QPaintEvent.rect())
class QExampleLabel (QLabel):
def __init__(self, parentQWidget = None):
super(QExampleLabel, self).__init__(parentQWidget)
self.initUI()
def initUI (self):
self.setPixmap(QPixmap('images/3.png'))
self.currentQRubberBand = MyRubberBand(QRubberBand.Rectangle, self)
def mousePressEvent (self, eventQMouseEvent):
print('mouse pressed')
self.originQPoint = eventQMouseEvent.pos()
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, QSize()))
self.currentQRubberBand.show()
def mouseMoveEvent (self, eventQMouseEvent):
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())
def mouseReleaseEvent (self, eventQMouseEvent):
print('mouse released')
# self.currentQRubberBand.hide()
currentQRect = self.currentQRubberBand.geometry()
# self.currentQRubberBand.deleteLater()
cropQPixmap = self.pixmap().copy(currentQRect)
cropQPixmap.save('output.png')
if __name__ == '__main__':
myQApplication = QApplication(sys.argv)
myQExampleLabel = QExampleLabel()
myQExampleLabel.resize(800, 600)
myQExampleLabel.show()
sys.exit(myQApplication.exec_())