Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to create a dynamic QLineEdit for a line joining two circles?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for pythonpython
2 Posts 2 Posters 419 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Disha
    wrote on last edited by
    #1

    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_())

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

      Hi and welcome to devnet,

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

      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
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved