*truly* simple example of painting?
-
wrote on 18 Mar 2022, 00:38 last edited by
Hi all -
I need to become acquainted with drawing/painting, in order to create graphical polygons within a display area. I've looked at this doc, but it seems anything but "basic."
Can someone point me to a truly simple example of, say, creating a green rectangle within a QWidget?
Thanks...
-
wrote on 18 Mar 2022, 04:31 last edited by
The hint is to become familiar with QPainter and how to override the paint() event for the widget. Therein is the majority of understanding...unless you want to paint with openGL, in which case plan to spend a couple years learning that.
-
Hi all -
I need to become acquainted with drawing/painting, in order to create graphical polygons within a display area. I've looked at this doc, but it seems anything but "basic."
Can someone point me to a truly simple example of, say, creating a green rectangle within a QWidget?
Thanks...
@mzimmers said in *truly* simple example of painting?:
Can someone point me to a truly simple example of, say, creating a green rectangle within a QWidget?
class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) {} protected: void paintEvent(QPaintEvent*) override { QPainter painter(this); painter.setBrush(Qt::green); painter.drawRect(QRect(10, 10, 400, 300)); } };
-
wrote on 14 Aug 2024, 03:50 last edited by
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor, QBrush
from PyQt5.QtCore import Qtclass GreenRectangleWidget(QWidget):
def init(self):
super().init()
self.initUI()def initUI(self): self.setGeometry(100, 100, 400, 300) # Set the window's position and size self.setWindowTitle('Green Rectangle') self.show() def paintEvent(self, event): painter = QPainter(self) painter.setBrush(QBrush(QColor(0, 255, 0), Qt.SolidPattern)) # Set brush color to green painter.drawRect(50, 50, 300, 200) # Draw a rectangle (x, y, width, height)
if name == 'main':
app = QApplication(sys.argv)
ex = GreenRectangleWidget()
sys.exit(app.exec_())