gpt4 book ai didi

python - PyQt5 自定义标签中不出现滚动条

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

几个月前,我想知道如何在图像上绘图( Insert Image into QGridLayout and Draw on top of image in PyQt5 )。 Eyllanesc 非常友善地给了我一个完整的解决方案。我现在想让这个图像小部件可滚动,以便我可以放大它。但是,当我添加滚动区域时,滚动条不会出现。当我强制滚动条出现时,它们似乎没有附加到任何东西。我看过很多关于滚动区域的帖子:/45515445/、/6792862/、/55149412/、/23446855/、/46979944/、/46024724/、/53843836/、/11886908/、/53914267/、/53843836/、/45515445/、/52671838/。这些解决方案不在 QGridLayout 中,但我不知道这是否是我的问题的根源。有人可以帮助我吗?谢谢!

from PyQt5.QtCore import (QPoint, Qt)
from PyQt5.QtGui import (QPainter, QPen, QPixmap)
from PyQt5.QtWidgets import (QApplication, QGridLayout, QMainWindow, QPushButton, QScrollArea, QTextEdit, QWidget) ####NEW
import os; import sys
###########################################################################
# https://stackoverflow.com/questions/56194201
class ImageLabel(QWidget):
# loads a graphic file, inserts it into a QLabel that allows drawing of lines on the surface
def __init__(self, parent=None):
super(ImageLabel, self).__init__(parent)
self.image = QPixmap('image.png')
self.drawing = False
self.lastPoint = QPoint()

def changePixmap(self,img, x, y):
self.image = QPixmap(img).scaled(x, y, Qt.KeepAspectRatio) #/34213021/, /21802868/
self.repaint()

def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(QPoint(), self.image)

def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()

def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(Qt.red, 1, Qt.SolidLine))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
self.update()

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = False

def sizeHint(self):
return self.image.size()
###########################################################################
class MainWindow(QMainWindow):
#########################################
def ZoomIn(self):
self.width = self.ilabel.image.size().width() *1.25
self.height = self.ilabel.image.size().height() * 1.25
self.ilabel.changePixmap('image.png', self.width, self.height)# SOF: /24106903/
########################################
def ZoomOut(self):
self.width = self.ilabel.image.size().width() / 1.25
self.height = self.ilabel.image.size().height() / 1.25
self.ilabel.changePixmap('image.png', self.width, self.height)# SOF: /24106903/
########################################
def __init__(self, parent = None):
super().__init__(parent)
# Set up main widget
widget = QWidget()
self.setCentralWidget(widget)
self.myLayout = QGridLayout(widget)
# Image
#######
self.width = 564; self.height = 800
self.ilabel = ImageLabel()

# https://stackoverflow.com/questions/45515445/
# Add this custom label to a scroll area
self.scroller = QScrollArea(self)
self.scroller.setWidget(self.ilabel)
self.scroller.setWidgetResizable(True)
self.scroller.adjustSize()
self.myLayout.addWidget(self.scroller, 0, 0, 110, 70)

# PROBLEM: NO SCROLLBARS APPEAR!
# If I add:
self.scroller.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn) #https://stackoverflow.com/questions/53914267/
self.scroller.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) # https://stackoverflow.com/questions/53914267/
# The scrollbars appear but do not scroll the image. I imagine they are not actually atached to the scroll area

# ZoomIn button
self.btnzi = QPushButton('Zoom In',self)
self.btnzi.clicked.connect(self.ZoomIn)
self.myLayout.addWidget(self.btnzi, 112, 5, 1, 1)
# ZoomOut button
self.btnzo = QPushButton('Zoom Out',self)
self.btnzo.clicked.connect(self.ZoomOut)
self.myLayout.addWidget(self.btnzo, 112, 10, 1, 1)
# Text
######
self.textedit = QTextEdit()
self.myLayout.addWidget(self.textedit, 0, 70, 111, 100)

if __name__ =='__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
###########################################################################

最佳答案

QScrollArea 使用 size() 和minimumSizeHint() 来设置何时需要滚动条,因此在您的情况下它是固定的,并且不依赖于导致问题的图像的大小。

class ImageLabel(QWidget):
# ...
def changePixmap(self,img, x, y):
self.image = QPixmap(img).scaled(x, y, Qt.KeepAspectRatio) #/34213021/, /21802868/
self.update()
self.resize(self.image.size())

def sizeHint(self):
return self.image.size()

def minimumSizeHint(self):
return self.sizeHint()

也没有必要强制显示滚动条,因此您必须删除:

self.scroller.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroller.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)

关于python - PyQt5 自定义标签中不出现滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601906/

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