Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to create a dynamic QLineEdit for a line joining two circles?



  • import random
    import sys
    import math
    from PyQt5.QtWidgets import QMenu
    from PyQt5 import QtCore, QtGui, QtWidgets, Qt
    from PyQt5.QtCore import QRect, QSize, QPoint, QLineF

    class Circle(QRect):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.line_to = self.line_from = None
    
    def join(self, other):
        self.line_to = other
        other.line_from = self
        if self.line_from and self.line_from.line_to == self:
            self.line_from.line_to = None
        self.line_from = other.line_to = None
    
    def Delete(self):
        pass
    def circlelabel(self, n):
        self.line_edit.setText('Circle ' + str(n))
        self.move_line_edit()
        self.line_edit.show()
    
    def move_line_edit(self):
        self.line_edit.move(self.topLeft().x(), self.topLeft().y() - 40)
    def linelabel(self,n):
        pass
    def move_obj_linetextbox(self):
        pass
    

    class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
    
        self.rect = QtCore.QRect()
        self.drag_position = QtCore.QPoint()
        self.circles = []
        self.labels=[]
        self.current_circle = None
        self.last_two_clicked = self.circles[:]
        self.linelabels=[]
        button = QtWidgets.QPushButton("Add", self)
        button.setIcon(QtGui.QIcon("addbutton.png"))
        button.setToolTip("<h3>This is for creating random circles<h3>")
        button.clicked.connect(self.on_clicked)
        joinb = QtWidgets.QPushButton("Join", self)
        joinb.setGeometry(100, 0, 100, 30)
        joinb.setIcon(QtGui.QIcon("circleicon.png"))
        joinb.setToolTip('This is for joining the two circles with a line')
        joinb.clicked.connect(self.joinAction)
    
        Delete = QtWidgets.QPushButton("Delete", self)
        Delete.setIcon(QtGui.QIcon("deleteicon.jpg"))
        Delete.clicked.connect(self.DeleteItem)
        Delete.setToolTip("<h3>This is for deleting the circle from canvas area<h3>")
        Delete.setGeometry(200, 0, 100, 30)
        Report= QtWidgets.QPushButton("Generate Report", self)
        Report.setGeometry(QRect(300, 0, 120, 30))
        Report.setIcon(QtGui.QIcon("generatereport.png"))
        Report.setToolTip("This is for generating pdf report of connection between two circles")
        Saveimg= QtWidgets.QPushButton("Save", self)
        Saveimg.setGeometry(QRect(420, 0, 100, 30))
        Saveimg.setIcon(QtGui.QIcon("saveicon.png"))
        Saveimg.setToolTip("This is for saving an image of canvas area")
    
        self.resize(640, 480)
    
    def on_clicked(self):
        coor = (random.randrange(self.width() - 100), random.randrange(self.height() - 100))
        c = Circle(*coor, 100, 100)
        text_box = QtWidgets.QLineEdit(self)
        c.line_edit = text_box
        self.labels.append(text_box)
        c.circlelabel(len(self.labels))
        self.circles.append(c)
    
        self.last_two_clicked.insert(0, c)
        self.last_two_clicked = self.last_two_clicked[:2]
        self.update()
    
    def joinAction(self, event):
        c1, c2 = self.last_two_clicked
        c1.join(c2)
        self.update()
    
    def DeleteItem(self):
        pass
    
    def contextMenuEvent(self, event):
    
        menu = QMenu("Circle")
        remove = menu.addAction("Delete Circle")
        #remove.triggered.connect(self.DeleteItem)
        a = menu.exec_(self.mapToGlobal(event.pos()))
        if a == remove:
            self.close()
    
    def paintEvent(self, event):
        super().paintEvent(event)
    
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setPen(QtGui.QPen(QtCore.Qt.black, 5, QtCore.Qt.SolidLine))
        for circle in self.circles:
            painter.drawEllipse(circle)
    
            if circle.line_to:
                painter.drawLine(circle.center(), circle.line_to.center())
    
    def mousePressEvent(self, event):
        for circle in self.circles:
            line = QLineF(circle.center(), event.pos())
            if line.length() < circle.width() / 2:
                self.current_circle = circle
                self.drag_position = event.pos()
                self.last_two_clicked.insert(0, circle)
                self.last_two_clicked = self.last_two_clicked[:2]
                break
    
    def mouseMoveEvent(self, event):
        if self.current_circle is not None:
            self.current_circle.translate(event.pos() - self.drag_position)
            self.current_circle.move_line_edit()
            self.drag_position = event.pos()
            self.update()
    
    def mouseReleaseEvent(self, event):
        self.current_circle = None
    

    if name == "main":
    app = QtWidgets.QApplication(sys.argv)
    Rect = Window()
    Rect.show()
    sys.exit(app.exec_())


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Rather than just posting code, you should also explain exactly what you want to achieve.


Log in to reply