滚动条不出现在 PyQt5 自定义标签中
Scrollbars Do Not Appear in PyQt5 Custom Label
几个月前我想知道如何在图像上绘图 ()。 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
###########################################################################
#
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()
#
# 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) #
self.scroller.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) #
# 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)
几个月前我想知道如何在图像上绘图 (
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
###########################################################################
#
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()
#
# 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) #
self.scroller.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) #
# 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)