def __init__(self): QMainWindow.__init__(self) # 'Ui_MyMainWindow' is the class, that was generated by uic, # naming convention is: 'Ui_' plus the name of root widget in designer self.ui = Ui_DisplayWindow() self.ui.setupUi(self)
class MyMainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) # 'Ui_MyMainWindow' is the class, that was generated by uic, # naming convention is: 'Ui_' plus the name of root widget in designer self.ui = Ui_DisplayWindow() self.ui.setupUi(self) @pyqtSlot() def btnStartClicked(self): self.ui.lstResults.clear() lst = run(self.ui.boxNb.text()) for i in range(0, len(lst)): strResult = " ".join(str(e) for e in lst[i]) self.ui.lstResults.addItem(strResult) def paintEvent(self, event): painter = QPainter(self) lstRect = self.ui.lstResults.rect() wndRect = self.rect() nbPoints = self.ui.boxNb.text() # définition de notre zone de dessin : à droite des listes/boutons et à 10 pixels de marge de chaque côté drawingArea = QRect(lstRect.right() + 20, 10, wndRect.width() - lstRect.width() - lstRect.x() - 30, wndRect.height() - 20) # on recadre la zone de dessin pour former un carré if (drawingArea.width() > drawingArea.height()): drawingArea.setWidth(drawingArea.height()) else: drawingArea.setHeight(drawingArea.width()) print "Drawing circle from", drawingArea.x(), "/", drawingArea.y(), "to", drawingArea.right(), "/", drawingArea.bottom() painter.drawEllipse(drawingArea) selection = self.ui.lstResults.selectedItems() if (selection == []): print "Selection vide" else: x = drawingArea.left() + drawingArea.width()/2 y = drawingArea.top() points = selection[0].text().split(" ") for i in range(1, len(points)): # calcul de l'angle par rapport à la verticale angle = ((360 / len(points)) * (int(points[i]) - 1)) # conversion par rapport à l'horizontale angle = (angle - 90) * -1 # conversion en radians (pour les fonctions cos() et sin() qui ne gèrent pas les degrés angle = angle * math.pi / 180 # on va calculer la position du point destination par rapport au centre du cercle # avec x et y définit à partir de la position centrale # sin(angle) = (y) / rayon => y = rayon * sin(angle) # cos(angle) = (x) / rayon => x = rayon * cos(angle) sinus = math.sin(angle) cosinus = math.cos(angle) destX = drawingArea.x() + drawingArea.width()/2 + (cosinus * (drawingArea.width()/2)) destY = drawingArea.y() + drawingArea.height()/2 - (sinus * drawingArea.height()/2) print "Angle = ", angle, " cos = ", cosinus, " sin = ", sinus print "Drawing line from ", x, "/", y, "to", destX, "/", destY painter.drawLine(x, y, destX, destY) x = destX y = destY print "Drawing (final) line from", x, "/", y, "to", drawingArea.x() + drawingArea.width()/2, "/", drawingArea.y() + 10 painter.drawLine(x, y, drawingArea.x() + drawingArea.width()/2, drawingArea.y())