boundingRect 的问题

wyjblue 2015-06-02 05:16:32
各位好,我要实现的功能是,MyItem继承QGraphicsItem

class MyItem(Qpoint(x1,y1),Qpoint(x2,y2))
{
mline=qlines(Qpoint(x1,y1),Qpoint(x2,y2));
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
QRectF boundingRect() const
{
qreal penWidth = 1;
return QRectF(mline.getX1() - penWidth / 2, mline.getY1() - penWidth / 2,
mline.getX2() + penWidth, mline.getY2() + penWidth);
}
实例化MyItem *nitem=new MyItem(Qpoint(x1,y1),Qpoint(x2,y2));
scene.additem(nitem);

绘制OK,但对单独图元操作出问题了,比如想移动一个线的item 选中一个对象进行移动 结果 那个对象带动它附近其他对象一起move
还有 我想实现 鼠标hover在图元上 与鼠标按下 出现如下效果

在 Myitem的paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/ )
{
......
if(option->state & QStyle::State_Selected)
{

painter->drawRect(boundingRect()); 选中后出现矩形框,但实际实验下来 整个乱套了
}
}
请问 这个线的boundingRect() 如何计算
上图中 两个效果如何设计
...全文
606 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyjblue 2015-06-08
  • 打赏
  • 举报
回复
谢谢你的回复,问题已经决解了,是我的边界值搞错了,所以没效果出来,是要做个图形编辑器,里面的一些图处理起来麻烦的很 哎。。。。
firebolt2002 2015-06-02
  • 打赏
  • 举报
回复
我刚上传了一个我写的demo,模仿drawcli写的,功能不完整,不过基本操作都有了,目前只创建矩形和椭圆,你可以把线也加上。 http://download.csdn.net/detail/firebolt2002/8765313
firebolt2002 2015-06-02
  • 打赏
  • 举报
回复
1、直线边界可以参考qt自带的例子程序 diagramscene。 QRectF Arrow::boundingRect() const { qreal extra = (pen().width() + 20) / 2.0; return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(), line().p2().y() - line().p1().y())) .normalized() .adjusted(-extra, -extra, extra, extra); } 2、你是不是想做个类似visio的图形控制点的效果呢? 这个比较麻烦,你可以参考一下mfc的例子drawcli。
较为完整的vc++ 画图程序 IMPLEMENT_SERIAL(CShape,CObject,1) IMPLEMENT_SERIAL(CLine,CShape,1) IMPLEMENT_SERIAL(CRectangle,CShape,1) IMPLEMENT_SERIAL(CCircle,CShape,1) IMPLEMENT_SERIAL(CCurve,CShape,1) IMPLEMENT_SERIAL(CEllipse,CShape,1) CShape::CShape(void) { } CShape::~CShape(void) { } CRect CShape::GetBoundRect() { CRect BoundingRect; // Object to store bounding rectangle BoundingRect = m_EnclosingRect; // Store the enclosing rectangle // Increase the rectangle by the pen width BoundingRect.InflateRect(m_Pen, m_Pen); return BoundingRect; // Return the bounding rectangle } void CShape::Serialize(CArchive& ar) { CObject::Serialize(ar); if(ar.IsStoring()) { ar<Rect<>m_Color>>m_Brush>>m_EnclosingRect>>m_Pen; } } CLine::CLine(void) { } CLine::~CLine(void) { } // CLine class constructor CLine::CLine(CPoint Start, CPoint End, COLORREF aColor,int PenWidth) { m_StartPoint = Start; // Set line start point m_EndPoint = End; // Set line end point m_Color = aColor; //m_Brush=aBrush;// Set line color m_Pen = PenWidth; // Set pen width // Define the enclosing rectangle m_EnclosingRect = CRect(Start, End); m_EnclosingRect.NormalizeRect(); } // Draw a CLine object void CLine::Draw(CDC* pDC) { // Create a pen for this object and // initialize it to the object color and line width of 1 pixel CPen aPen; if(!aPen.CreatePen(PS_SOLID, m_Pen, m_Color)) { // Pen creation failed. Abort the program AfxMessageBox(_T("Pen creation failed drawing a line"), MB_OK); AfxAbort(); } CPen* pOldPen = pDC->SelectObject(&aPen;); // Select the pen // Now draw the line pDC->MoveTo(m_StartPoint); pDC->LineTo(m_EndPoint); pDC->SelectObject(pOldPen); // Restore the old pen } void CLine::Serialize(CArchive& ar) { CShape::Serialize(ar); if(ar.IsStoring()) { ar<>m_StartPoint>>m_EndPoint; } }

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧