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?
QtWS25 Last Chance

*truly* simple example of painting?

Scheduled Pinned Locked Moved Solved Brainstorm
4 Posts 4 Posters 806 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 18 Mar 2022, 00:38 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...

    J 1 Reply Last reply 18 Mar 2022, 15:18
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 18 Mar 2022, 04:31 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
      • M mzimmers
        18 Mar 2022, 00:38

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

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 18 Mar 2022, 15:18 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 14 Aug 2024, 03:50 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