qt怎么在qlabel中选中两点并连线
时间: 2023-05-23 14:02:01 浏览: 204
您可以尝试使用 QGraphicsView 和 QGraphicsScene 来实现在 QLabel 中选中两个点并将其连接起来。您可以使用 QGraphicsView 来呈现视图,QGraphicsScene 用于维护场景和对象,以及 QGraphicsLineItem 用于绘制线段。以下是示例代码:
```python
from PyQt5.QtWidgets import QGraphicsLineItem, QGraphicsScene, QGraphicsView, QApplication, QMainWindow, QLabel
class LineEditor(QMainWindow):
def __init__(self):
super().__init__()
# create a label to display the image
self.label = QLabel(self)
self.setCentralWidget(self.label)
# create a QGraphicsView and QGraphicsScene
self.view = QGraphicsView(self)
self.scene = QGraphicsScene(self)
self.view.setScene(self.scene)
self.setCentralWidget(self.view)
# add two points and a line connecting them
self.p1 = self.scene.addEllipse(0, 0, 10, 10)
self.p2 = self.scene.addEllipse(50, 50, 10, 10)
self.line = QGraphicsLineItem(self.p1.boundingRect().center().x(),
self.p1.boundingRect().center().y(),
self.p2.boundingRect().center().x(),
self.p2.boundingRect().center().y())
self.scene.addItem(self.line)
if __name__ == '__main__':
app = QApplication([])
window = LineEditor()
window.show()
app.exec_()
```
这将在 QLabel 中显示一个视图,并在 QGraphicsScene 中添加两个点和一条连接它们的线。如果您希望用户能够选择两个点并将它们连接起来,您可以添加一些交互功能,例如使用 QGraphicsItem.mousePressEvent() 方法来捕捉鼠标事件并移动点,或者添加一个按钮来启用点选择模式。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)