我有一个自定义的qtextedit(称为testLabel)。我这样做是为了修改普通的qtextedit以支持某种情绪,并使其高度与文本一致。 现在我的问题是,当我在Scroll区域之外使用此testLabel时,它工作良好, 但是当我将它放在滚动区域内时,testlabel的内容是可滚动的!
我试图关闭testlabel的滚动条策略。但是它只隐藏了滚动条。向上移动滚轮仍会使内容滚动。
这是testlabel的相关部分(我删除了不必要的部分,例如情绪处理程序)
class testLabel(QTextEdit):
def __init__(self,text,direction,chatMsgType):
super(testLabel, self).__init__()
self.setReadOnly(True)
self.setAcceptRichText(True)
#self.setDisabled(True)
sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(20)
sizePolicy.setVerticalStretch(0)
self.setSizePolicy(sizePolicy)
self.setLineWrapMode(QTextEdit.FixedColumnWidth)
self.setLineWrapColumnOrWidth(35)
self.color = "#2ECC71"
self.direction = "me"
def paintEvent(self, e):
try:
r=self.viewport()
p = QPainter(r)
p.setRenderHint(QPainter.Antialiasing, False)
if self.direction=="me":
rect = QRectF(6, 0, self.width() - 6, self.height() - 1)
else:
rect = QRectF(0, 0, self.width() - 6, self.height() - 1)
self.adjustSize()
p.setPen(Qt.NoPen)
path = QPainterPath()
path.setFillRule(Qt.WindingFill)
path.addRoundedRect(rect, 4, 4)
linePath = QPainterPath()
if self.direction == "me":
linePath.moveTo(0-1, rect.height() / 2)
linePath.lineTo(8-1, 8 + rect.height() / 2)
linePath.lineTo(8-1, -8 + rect.height() / 2)
else:
linePath.moveTo(rect.width()+6, rect.height() / 2)
linePath.lineTo(rect.width() , 8 + rect.height() / 2)
linePath.lineTo(rect.width() , -8 + rect.height() / 2)
path = path.united(linePath)
p.fillPath(path, QColor(self.color))
super(testLabel, self).paintEvent(e)
except Exception as e:
debug.Error(str(e))
traceback.print_exc()
def resizeEvent(self, e):
try:
super(testLabel, self).resizeEvent( e)
docHeight = self.document().size().height()
self.setFixedHeight(docHeight)
if len(self.toPlainText()) < 35:
self.document().adjustSize()
w=self.fontMetrics().boundingRect(self.toPlainText()).width()
self.setMaximumWidth(w+16+16+self.nEmotions*16)
#self.nEmotions = no of emotions in text
except Exception as e:
traceback.print_exc()
以下是qscrollarea内外使用的同一类的结果。