• 5852阅读
  • 4回复

[讨论]QGraphicsRectItem的焦点问题 [复制链接]

上一主题 下一主题
离线yanzh
 

只看楼主 倒序阅读 楼主  发表于: 2013-11-06
我自定义了QGraphicsRectItem类,在场景里添加该多个对象,用鼠标选中其中一个后,为什么没有画出周围的虚线框(表示被选中)呢?boundingRect()虚函数被重写了啊?请知道的指点下,谢谢
CCustomRectItem::CCustomRectItem(ItemType Type, QMenu *pMenu,
                         QGraphicsItem *parent, QGraphicsScene *scene)
    :QGraphicsRectItem(parent,scene)
{
    itype = Type;
    contextMenu = pMenu;
    m_cursor = new QCursor;
    switch (Type)
    {
    case Protection:
        mRect = QRectF(-50,-70,100,140);
        setRect(mRect);
        itemColor = QColor(250,204,167);
        break;
    case SignalDevice:
        mRect = QRectF(-40,-40,80,80);
        setRect(mRect);
        itemColor = QColor(152,208,222);
        break;
    default:
        break;
    }
    setRect(mRect);
    setZValue(0);
    setAcceptDrops(true);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    setFlag(QGraphicsItem::ItemIsFocusable,true);
    setAcceptsHoverEvents(true);


    cScale = false;
    zoomFlag = false;


}


void CCustomRectItem::ResetRect(QRectF rect)
{
    mRect = rect;
    update(boundingRect());


}
QRectF CCustomRectItem::boundingRect()
{
    return QRectF(mRect.x()-2,mRect.y()-2,mRect.width()+4,mRect.height()+4);
   // return QRectF(mRect);


}void CCustomRectItem::paint(QPainter *painter, QStyleOptionGraphicsItem *option, QWidget *widget)
{
//    Q_UNUSED(option);
//    Q_UNUSED(widget);
    painter->save();
    painter->setBrush(QBrush(itemColor));
    painter->drawRect(mRect);
    painter->restore();


}
//实现该虚函数,该函数会被基类调用,用于设置Item的形状,以及其他碰撞检测等用途
QPainterPath CCustomRectItem::shape()
{
    QPainterPath path;
    path.addRect(mRect);
    return path;
}

void CCustomRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(cScale)
        {
            start = event->scenePos();
            zoomFlag = true;
            update();
            QGraphicsItem::mousePressEvent(event);
        }
    }
}
void CCustomRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(zoomFlag)
    {
        QPointF dis;
        end = event->scenePos();
        dis=end-start;
        start=end;
        QRectF tem;
        prepareGeometryChange();
        switch (direction)
        {
        case 1:
            tem = QRectF(mRect.x()+dis.x()/2,mRect.y()+dis.y()/2,mRect.width()+dis.x(),mRect.height()+dis.y());
            this->ResetRect(tem);
            update(boundingRect());
            this->moveBy(-dis.x()/2,-dis.y()/2);
            break;
        case 2:
            tem = QRectF(mRect.x(),mRect.y()+dis.y()/2,mRect.width(),mRect.height()+dis.y());
            this->ResetRect(tem);
            update(boundingRect());
            this->moveBy(0,-dis.y()/2);
            break;
        case 3:
            tem = QRectF(mRect.x()+dis.x()/2,mRect.y(),mRect.width()+dis.x(),mRect.height());
            this->ResetRect(tem);
            update(boundingRect());
            this->moveBy(-dis.x()/2,0);
            break;
        default:
            break;
        }


        update();
    }
    else
    {
        QGraphicsItem::mouseMoveEvent(event);
    }


}
void CCustomRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    cScale = false;
    zoomFlag = false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);


}
离线yanzh

只看该作者 1楼 发表于: 2013-11-06
c-o-n-s-t怎么编程敏感词了? 上面代码有c-o-n-s-t的地方都删除了
离线dbzhang800

只看该作者 2楼 发表于: 2013-11-06
应该是,因为你重新实现的 paint() 中没有画这个框的代码
离线yanzh

只看该作者 3楼 发表于: 2013-11-06
谢谢,应该是这里的问题
离线friendbaby

只看该作者 4楼 发表于: 2013-11-06
同2楼,在paint函数中,判断是否被选中,选中就把pen设置为实线,否则设置为虚线
Smiling is best language , can express everything , also can conceal everything.
快速回复
限100 字节
 
上一个 下一个