I am drawing in PyQt5 inside a QDialog box but need to add the ability to scroll as I add to the objects drawn. It seems like the best option maybe to add a QScrollArea widget and draw inside that but I haven't been able to find anything telling me how to do so. I was able to find this question that seems to be on topic, but is for C++ and I don't understand it.
Qt - draw inside QScrollArea in a QDialog
Would someone be able to translate what this means into Python or explain what would be necessary to draw inside a QScrollArea widget or add scroll bars to a QDialog?
Each class has a single responsibility, in this case there must be 2 widgets: the widget where it is drawn and the widget that has the QScrollArea:
from PyQt5 import QtCore, QtGui, QtWidgets
class DrawWidget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(DrawWidget, self).__init__(*args, **kwargs)
self.setFixedSize(640, 480)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setBrush(QtGui.QBrush(QtCore.Qt.darkMagenta))
painter.setPen(QtCore.Qt.NoPen)
path = QtGui.QPainterPath()
path.addText(QtCore.QPoint(10, 100), QtGui.QFont("Times", 40, QtGui.QFont.Bold), "Stack Overflow and Qt")
painter.drawPath(path)
class Dialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
scroll_area = QtWidgets.QScrollArea(widgetResizable=True)
draw_widget = DrawWidget()
scroll_area.setWidget(draw_widget)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(scroll_area)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.resize(320, 240)
w.show()
sys.exit(app.exec_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With