概述
用Qt鼠標事件實現(xiàn)基本幾何圖形的繪制,支持直線、矩形、圓形、橢圓。后期可以在此基礎上進行擴展。
效果圖

實現(xiàn)
本示例使用QGraphics體系來實現(xiàn),因為要移動對象,所以生成的圖形必須是一個單獨的對象,鼠標拖動繪制的過程是在臨時層中完成,release后生成一個矢量的圖形item并添加到場景中。
關鍵代碼
主場景中有一個父rootItem,在scene中將鼠標或觸控事件傳到rooitem后動態(tài)繪制臨時的圖形,release事件后生成一個標準的圖形對象:
本示例使用QGraphics體系來實現(xiàn),因為要移動對象,所以生成的圖形必須是一個單獨的對象,鼠標拖動繪制的過程是在臨時層中完成,release后生成一個矢量的圖形item并添加到場景中。
關鍵代碼
主場景中有一個父rootItem,在scene中將鼠標或觸控事件傳到rooitem后動態(tài)繪制臨時的圖形,release事件后生成一個標準的圖形對象:
void GsRootItem::drawPress(int id, const QPointF &p)
{
ShapeInfo info;
info.firstPos = p;
info.type = getCurType();
m_Objs.insert(id,info);
}
void GsRootItem::drawMove(int id, const QPointF &lastPoint, const QPointF &curPoint)
{
if(!m_Objs.contains(id)){
return;
}
ShapeInfo info = m_Objs.value(id);
m_pTempLayer->drawShape(info.type,info.firstPos,curPoint);
}
void GsRootItem::drawRelease(int id, const QPointF &point)
{
if(!m_Objs.contains(id)){
return;
}
ShapeInfo info = m_Objs.value(id);
drawRealShape(info.type,info.firstPos,point);
m_Objs.remove(id);
m_pTempLayer->clear();
}
...
void GsRootItem::drawRealShape(GsShapeType type, QPointF p1, QPointF p2)
{
//計算圖形繪制區(qū)域
QRectF rect;
rect.setX(qMin(p1.x(),p2.x()));
rect.setY(qMin(p1.y(),p2.y()));
if(type == Shape_Circle){
rect.setWidth(qAbs(p1.y() - p2.y()));
rect.setHeight(qAbs(p1.y() - p2.y()));
}
else{
rect.setWidth(qAbs(p1.x() - p2.x()));
rect.setHeight(qAbs(p1.y() - p2.y()));
}
rect.adjust(-5,-5,5,5);
GsShapeBaseItem * item = m_pShapeFactory->getShapeItem(type,rect,this);
item->drawShape(p1,p2);
}
drawRealShape函數(shù)就是用于創(chuàng)建一個獨立的幾何圖形,通過以下的工廠模式來生成
GsShapeBaseItem * item = m_pShapeFactory->getShapeItem(type,rect,this);
工廠代碼:
GsShapeBaseItem *GsShapeFactory::getShapeItem(GsShapeType type,QRectF rectF,
QGraphicsObject *parent)
{
GsShapeBaseItem * item = nullptr;
switch (type) {
case Shape_Line:
item = new GsShapeLineItem(rectF,parent);
break;
case Shape_Rectange:
item = new GsShapeRectangeItem(rectF,parent);
break;
case Shape_Circle:
item = new GsShapeCircleItem(rectF,parent);
break;
case Shape_Oval:
item = new GsShapeOvalItem(rectF,parent);
break;
default:
break;
}
item->setZValue(10);
return item;
}
在工廠類中會創(chuàng)建不同的圖形對象。每一個圖形對象是繼承于QGraphicsObject然后重寫paint函數(shù)去進行繪制,比如說原型:
void GsShapeCircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
QColor color = Qt::red;//(rand()%255,rand()%255,rand()%255);
painter->setBrush(color);
if(m_bTap){
painter->setPen(QPen(Qt::yellow,5,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
}
else{
painter->setPen(QPen(color,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
}
painter->drawEllipse(m_firstPoint.x(),m_firstPoint.y(),
qAbs(m_lastPoint.y() - m_firstPoint.y()),
qAbs(m_lastPoint.y() - m_firstPoint.y()));
}
其他圖形類似。
實現(xiàn)圖形的選擇和拖動,需要在item中添加以下兩句:
setFlag(ItemIsSelectable,true);
setFlag(ItemIsMovable,true);
然后就可以自由拖動啦。文章來源:http://www.zghlxwxcb.cn/news/detail-645755.html
基本邏輯都很簡單。文章來源地址http://www.zghlxwxcb.cn/news/detail-645755.html
到了這里,關于Qt鼠標拖動繪制基本幾何圖形的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!