Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. *truly* simple example of painting?

*truly* simple example of painting?

Scheduled Pinned Locked Moved Solved Brainstorm
4 Posts 4 Posters 814 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    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...

    JKSHJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • mzimmersM mzimmers

        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...

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by JKSH
        #3

        @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));
            }
        };
        

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4
        • J Offline
          J Offline
          Jonmaker
          wrote on last edited by
          #4

          import sys
          from PyQt5.QtWidgets import QApplication, QWidget
          from PyQt5.QtGui import QPainter, QColor, QBrush
          from PyQt5.QtCore import Qt

          class 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_())

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved