1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class preview(QGroupBox):
def __init__(self, width=400, height=300):
super().__init__()
self.__margin = 5
self.__width = width # Inutile
self.__height = height # Inutile
self.setTitle('Preview')
canvas = QPixmap(width, height)
canvas.fill(Qt.white)
self.__label = QLabel()
self.__label.setPixmap(canvas)
layout = QVBoxLayout(self)
layout.addWidget(self.__label)
#self.draw(5, 5, 290, 290)
def draw(self, x, y, width, height, color='black'):
print("draw")
pen = QPen()
pen.setWidth(3)
pen.setColor(QColor(color))
painter = QPainter(self.__label.pixmap())
painter.setPen(pen)
painter.drawRect(x, y, width, height)
painter.end()
self.update()
class myApp(QMainWindow):
''' Fenêtre principale de l'interface graphique
'''
def __init__(self, parent=None):
super().__init__(parent, Qt.WindowFlags())
self.setCentralWidget(QWidget(None, Qt.WindowFlags()))
self.__chartPreview = preview()
self.btntest = QPushButton('Test', clicked=self.test)
mainLayout = QVBoxLayout(self.centralWidget())
mainLayout.addWidget(self.__chartPreview)
mainLayout.addWidget(self.btntest)
def test(self):
print("test")
self.__chartPreview.draw(5, 5, 290, 290)
import sys
app=QApplication(sys.argv)
app.w=myApp()
app.w.show()
app.exec_() |
Partager