天天看点

Qt图形视图框架--精确选中连接线

一、前言

在使用Qt图形视图框架进行组态软件开发过程中,我通过继承自QGraphicsPathItem自定义的连接线图元Arrow,发现点选连接线特别不灵敏;

class Arrow : public QGraphicsPathItem {
public:
    enum { Type = UserType + TYPE_ARROW };
}      

解决方案:重写​

​QPainterPath shape() const​

​函数

二、效果展示

三、具体代码

QPainterPath Arrow::shape() const
{
    QPainterPath path(*points[0]);
    for(int i=1; i<points.length(); i++) {
        path.lineTo(*points[i]);
    }
    QPainterPathStroker stroker;
    stroker.setWidth(50);   //通过设置宽度来扩大选择的范围
    stroker.setJoinStyle(Qt::MiterJoin);
    stroker.setCapStyle(Qt::RoundCap);
    stroker.setDashPattern(Qt::DashLine);
    return stroker.createStroke(path);
}      

继续阅读