Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. QPainter not active

QPainter not active

Scheduled Pinned Locked Moved Unsolved Language Bindings
pyqt5python
4 Posts 2 Posters 13.0k 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.
  • U Offline
    U Offline
    undefined
    wrote on last edited by A Former User
    #1

    Hello,

    I am making a simple program in Python with PyQT 5 for GUI, which is supposed to draw a square onto the screen.

    However, I am quite confused as to how I am supposed to set up my QPainter.

    I have the following code:

    from PyQt5 import QtWidgets, QtGui, QtCore
    import sys
    
    def paintEvent(self, x, y, color, event = None):
        global qp
        qp = QtGui.QPainter(self);
        qp.begin(self)
        qp.setPen(QtGui.QColor(int(color, 16)))
        qp.setBrush(QtGui.QColor(int(color, 16)))
        qp.drawRect(x, y, 25, 25)
        qp.end()
    
    def window(title, width, height):
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QWidget()
        w.setFixedSize(width, height)
        w.setWindowTitle(title)
        paintEvent(w, 0, 0, 'ffffff')
        w.show()
        sys.exit(app.exec_())
    
    window('test', 120, 120)
    

    But this gives me:

    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active
    QPainter::setBrush: Painter not active
    QPainter::drawRects: Painter not active
    QPainter::end: Painter not active, aborted
    

    Can someone point me in the right direction?

    Thanks,
    Matt

    P.S First post. new to Qt - don't be harsh ;)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      If you want to do custom painting, you have to create a custom QWidget subclass and reimplement the paintEvent method of that class. You can't just freely paint from anywhere at any time.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • U Offline
        U Offline
        undefined
        wrote on last edited by
        #3

        I don't quite know how to do that, could you expand on that, please?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          A quick google search returns this article which covers most aspects of this subject.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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