Qt 子类化QRubberBand时如何使paintEvent工作?
目前,我正在使用PySide在Ubuntu14.04LTS下进行图像裁剪。我在这篇文章中遇到了一个问题,它描述了如何进行图像裁剪的一个粗略想法。现在我想更改裁剪矩形的宽度和颜色。接下来,我知道我应该创建一个类,该类将Qt 子类化QRubberBand时如何使paintEvent工作?,qt,qt4,pyside,Qt,Qt4,Pyside,目前,我正在使用PySide在Ubuntu14.04LTS下进行图像裁剪。我在这篇文章中遇到了一个问题,它描述了如何进行图像裁剪的一个粗略想法。现在我想更改裁剪矩形的宽度和颜色。接下来,我知道我应该创建一个类,该类将QRubberBand子类化,并重写paintEvent方法。因此,我在底部得到了python代码。如您所见,我在paintEvent中使用了pen.setWidth(5)。但是,当我进行图像裁剪时,线宽不会改变(默认情况下,似乎保持1)。请参见下面的红色矩形 请帮忙。谢谢 导入系
QRubberBand
子类化,并重写paintEvent
方法。因此,我在底部得到了python
代码。如您所见,我在paintEvent
中使用了pen.setWidth(5)
。但是,当我进行图像裁剪时,线宽不会改变(默认情况下,似乎保持1
)。请参见下面的红色矩形
请帮忙。谢谢
导入系统
从PyQt4导入QtGui、QtCore
MyRubberBand类(QtGui.QRubberBand):
def uuu init uuuu(self,QRubberBand_形状,QWidget_parent=None):
超级(MyRubberBand,self)。\uuuu初始(QRubberBand\u形状,QWidget\u父项)
palete=QtGui.qpalete()
setBrush(QtGui.qpalete.WindowText、QtGui.QBrush(QtCore.Qt.red))
self.setPalette(调色板)
def paintEvent(自我、QPaintEvent):
painter=QtGui.QStylePainter(self)
#画家。开始(自我)
pen=QtGui.QPen()
#笔设置样式(QtCore.Qt.SolidLine)
#pen.setColor(QtGui.QColor(QtCore.Qt.red))
笔设置宽度(5)
画师:画笔
option=QtGui.QStyleOptionFocusRect()
option.initFrom(self)
painter.drawControl(QtGui.QStyle.CE_FocusFrame,选项)
类QExampleLabel(QtGui.QLabel):
def uuu init uuuu(self,parentQWidget=None):
超级(QExampleLabel,self)。\uuuu初始化(parentQWidget)
self.initUI()
def initUI(self):
self.setPixmap(QtGui.QPixmap('images/3.png'))
self.currentQRubberBand=MyRubberBand(QtGui.QRubberBand.Rectangle,self)
def鼠标压力事件(self、eventQMouseEvent):
打印“鼠标按下”
self.originQPoint=eventQMouseEvent.pos()
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint,QtCore.QSize())
self.currentQRubberBand.show()
def mouseMoveEvent(self、eventqmousevent):
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint,eventQMouseEvent.pos()).normalized())
def mouseReleaseEvent(self、eventQMouseEvent):
打印“鼠标释放”
#self.currentQRubberBand.hide()
currentQRect=self.currentQRubberBand.geometry()
#self.currentQRubberBand.deleteLater()文件
cropQPixmap=self.pixmap().copy(currentQRect)
cropQPixmap.save('output.png')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
myQApplication=QtGui.QApplication(sys.argv)
myQExampleLabel=QExampleLabel()
myQExampleLabel.show()
sys.exit(myQApplication.exec)
我知道这个问题很老了,但我最近也在尝试做同样的事情,并且看到了这个问题,所以我想为其他可能遇到这个问题的人发布一些代码 您的代码已更新
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_())