在qt中使用c++,用折线图元进行连线,将两个图元进行连接,并且鼠标拖动图元,折线可以跟着移动变化的效果,把头文件也带上完整一点
时间: 2023-12-10 11:06:00 浏览: 80
以下是一个基本的实现,包含Qt中的QGraphicsItem和QGraphicsScene:
```
#include <QtWidgets>
class CustomItem : public QGraphicsItem
{
public:
CustomItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent) {}
// 定义item矩形区域,用于限制item的显示区域和碰撞检测
QRectF boundingRect() const override
{
return QRectF(-20, -20, 40, 40);
}
// 绘制item
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
painter->drawEllipse(-20, -20, 40, 40);
}
// 处理item的拖动事件
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
{
QGraphicsItem::mouseMoveEvent(event);
update();
}
};
class CustomLine : public QGraphicsLineItem
{
public:
CustomLine(QGraphicsItem *startItem, QGraphicsItem *endItem, QGraphicsItem *parent = nullptr) : QGraphicsLineItem(parent), m_startItem(startItem), m_endItem(endItem)
{
setPen(QPen(Qt::black, 2));
updatePosition();
}
// 更新线的位置
void updatePosition()
{
setLine(QLineF(m_startItem->pos(), m_endItem->pos()));
}
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
updatePosition();
QGraphicsLineItem::paint(painter, option, widget);
}
private:
QGraphicsItem *m_startItem;
QGraphicsItem *m_endItem;
};
class CustomScene : public QGraphicsScene
{
public:
CustomScene(QObject *parent = nullptr) : QGraphicsScene(parent) {}
// 创建item和线
void createItemAndLine()
{
CustomItem *item1 = new CustomItem();
CustomItem *item2 = new CustomItem();
addItem(item1);
addItem(item2);
CustomLine *line = new CustomLine(item1, item2);
addItem(line);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsView view;
CustomScene scene;
view.setScene(&scene);
view.show();
scene.createItemAndLine();
return app.exec();
}
```
在这个实现中,我们自定义了两个类,`CustomItem`和`CustomLine`,分别代表图元和连接线。`CustomItem`继承自`QGraphicsItem`,用于表示图元;`CustomLine`继承自`QGraphicsLineItem`,用于表示连接线。
在`CustomScene`中,我们创建了两个`CustomItem`对象,并将它们添加到场景中。然后创建了一个`CustomLine`对象,并将它也添加到场景中。在创建连接线时,我们将起点和终点的图元作为参数传递给`CustomLine`的构造函数,这样连接线就能够知道起点和终点的位置了。
在`CustomLine`中,我们重载了`paint`函数,这样每当场景刷新时,连接线的位置也会被更新。
在`CustomItem`中,我们重载了`mouseMoveEvent`函数,这样每当图元被拖动时,它会通知连接线更新位置。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)