• 8474阅读
  • 6回复

如何实现画折线? [复制链接]

上一主题 下一主题
离线hnwdx
 

只看楼主 倒序阅读 楼主  发表于: 2011-08-05
我利用
QGraphicsItemGroup
将QGraphicsLineItem加进去,
m_path.addPath(pitem->shape()); // pitem为一段线
addToGroup(pitem);
然后重载 QPainterPath GraphicsLineItemGroup::shape() const
{
QPainterPathStroker stroker;
stroker.setWidth(4.0);
return stroker.createStroke(m_path);
}
实现是实现了,但结果是当使用橡皮筋拖动选择该Group时,鼠标会卡壳,CPU占用率高。
shape() 换回去后就不卡,然而却实现不了点住线才选择。成了矩形选择,我该怎么办才合适?
谢谢了,求高手指点。
离线hnwdx

只看该作者 1楼 发表于: 2011-08-07
郁闷死,最后不用QT提供的橡皮筋选择功能,自己做选择,性能提高了。
但QGraphicsItemGroup中的单根线被移动后QGraphicsItemGroup就不能正确显示了,难道需要重新把线全部清除掉,再加进来。
离线hnwdx

只看该作者 2楼 发表于: 2011-08-08

如图,我使用QGraphicsItemGroup画出来的曲线,由多个线段组合的,也可以拖动,但如何用我鼠标操作的虚线段替代QGraphicsItemGroup中的相应实线段?
求高人,谢谢了。
离线hnwdx

只看该作者 3楼 发表于: 2011-08-08
重载了QGraphicsItemGroup的两个函数总算搞定了,就差最后一个问题了,就是需要刷新一下,这个问题是老问题了,调用update不管用,目的是触发那个重载的boundingRect()函数。
// 显示区域
QPainterPath GraphicsLineItemGroup::shape() const
{
    QPainterPathStroker stroker;
    QList<QGraphicsItem *> phead = childItems();
    int i, num;
    QPainterPath lpath;
    num = phead.count();
    stroker.setWidth(4.0);
    for (i=0; i<num; i++)
    {
        lpath.addPath(phead->shape());
    }
    return stroker.createStroke(lpath);
}

// 可选择的区域
QRectF GraphicsLineItemGroup::boundingRect() const
{
    QList<QGraphicsItem *> phead = childItems();
    int i, num, ix1, ix2, iy1, iy2;
    num = phead.count();
    if (num==0)
    {
        return QGraphicsItemGroup::boundingRect();
    }
    ix1 = phead[0]->boundingRect().left();
    iy1 = phead[0]->boundingRect().top();
    ix2 = phead[0]->boundingRect().right();
    iy2 = phead[0]->boundingRect().bottom();
    for (i=0; i<num; i++)
    {
        if (phead->boundingRect().left()<ix1)
        {
            ix1 = phead->boundingRect().left();
        }
        if (phead->boundingRect().top()<iy1)
        {
            iy1 = phead->boundingRect().top();
        }
        if (phead->boundingRect().right()>ix2)
        {
            ix2 = phead->boundingRect().right();
        }
        if (phead->boundingRect().bottom()>iy2)
        {
            iy2 = phead->boundingRect().bottom();
        }
    }
    QPointF p(3,3);
    return QRectF(QPointF(ix1, iy1)-p, QPointF(ix2, iy2)+p);
}

有什么办法吗??
离线hnwdx

只看该作者 4楼 发表于: 2011-08-11

自己画,自己解说,自己结贴。
BoundingRect改变后,调用prepareGeometryChange(),就可以了。
离线guguclk
只看该作者 5楼 发表于: 2011-09-07
我来回一个,楼主强悍
离线ansly20060

只看该作者 6楼 发表于: 2012-11-29
楼主强悍啦,那个如何实现拖动item到group中就自动组合了啊。我也要花折线,想通过鼠标点击画,该怎么办呢。。。大神请赐教啊
快速回复
限100 字节
 
上一个 下一个