1

I'm making an application, which should draw several points on widget and connect some of them with lines. I've made a form using QT Designer and I want to draw the points on frame for example. I've read that to draw on a widget its paintEvent() method should be reimplemented and I have a problem with it. My MainForm class has following code:

.........
def paintEvent(self, QPaintEvent):
    paint = QtGui.QPainter()
    paint.begin(self)
    paint.setPen(QtCore.Qt.red)
    size = self.size()
    for i in range(100):
       x = random.randint(1, size.width()-1)
       y = random.randint(1, size.height()-1)
       paint.drawPoint(x, y)
    paint.end()
............

That method draws points on main window. How to make paintEvent() draw on exact frame of my form? And one more question: how make it only when I press some button because the code above redraws my window after any event.

I use PyQt v4.10 and Python 3.3 if it's important.

Thanks in advance for any help.

Skycker
  • 23
  • 1
  • 5

1 Answers1

1

I've solved my problem so: I create my own widget (called PaintSpace) and put it into layout on my main form. Following code is inside MainForm class:

    class MyPaintSpace(QtGui.QWidget):
                """My widget for drawing smth"""

                def __init__(self):
                    super(PaintSpace, self).__init__()

                    <some code>

                def paintEvent(self, QPaintEvent):
                    """Reimpltmented drawing method of my widget"""

                    paint = QtGui.QPainter()
                    paint.begin(self)

                    <smth we want to draw>

                    paint.end()

    # Make an object...
    self.myPaintSpaceYZ = MyPaintSpace()
    # ...and put it in layout
    self.verticalLayoutYZ.addWidget(self.myPaintSpaceYZ)

After that to redraw my widget I use .update() method.

Skycker
  • 23
  • 1
  • 5
  • so you put a class inside of a class? and is PaintSpace the name of the QWidget in your QT designer? and what is verticalLayoutYZ? – jerbotron Mar 03 '15 at 17:08
  • @jerbotron, no, it is not a class inside of a class. Just another class. See this question for more examples: https://stackoverflow.com/questions/8993842/pyqt-drawing-on-an-exsiting-widget-of-gui – Marcos Saito Feb 12 '19 at 01:40