我想使用qquickpainteditem類來在qml上繪制圖形,之前有實作過繪圖功能
這次我照葫蘆畫瓢再寫一遍,發現行不通了,試過很多方法怎么都顯示沒呼叫過paint()函式,
想到由于它是繼承的qquickitem,我就重寫updatepaintnode(),發現他有呼叫這個函式,而不是調我想要的paint(),這讓我很奇怪
上代碼
roi.h:
class rectRoi : public QQuickPaintedItem
{
Q_OBJECT
public:
rectRoi(QQuickItem *parent = 0);
virtual ~rectRoi() Q_DECL_OVERRIDE;
protected:
void paint(QPainter *painter) Q_DECL_OVERRIDE;
QRectF boundingRect() const Q_DECL_OVERRIDE;
QSGNode *updatePaintNode(QSGNode *Node,UpdatePaintNodeData*) override;
private:
QRectF Rect, mBoundingRect;
QPen PointPen, LinePen;
void setRect(const int &x, const int &y, const int &w, const int &h);
};
roi.cpp:
rectRoi :: rectRoi(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
PointPen.setColor("red");
PointPen.setWidth(9);
LinePen.setColor("red");
LinePen.setStyle(Qt::SolidLine);
LinePen.setWidth(3);
setRect(300,200,200,100);
// setFlag(QQuickItem::ItemHasContents);
}
rectRoi::~rectRoi()
{
}
QSGNode *rectRoi::updatePaintNode(QSGNode *Node,UpdatePaintNodeData*)
{
qDebug() << "updatePaintNode";
return Node;
}
void rectRoi::paint(QPainter *painter)
{
qDebug()<<"paint";
painter->setRenderHint(QPainter::Antialiasing,true);
painter->setPen(PointPen);
painter->drawPoint(Rect.topLeft());
painter->drawPoint(Rect.topRight());
painter->drawPoint(Rect.bottomLeft());
painter->drawPoint(Rect.bottomRight());
painter->setPen(LinePen);
painter->drawLine(Rect.topLeft(),Rect.topRight());
painter->drawLine(Rect.topRight(),Rect.bottomRight());
painter->drawLine(Rect.bottomRight(),Rect.bottomLeft());
painter->drawLine(Rect.bottomLeft(),Rect.topLeft());
}
QRectF rectRoi::boundingRect() const
{
return mBoundingRect;
}
void rectRoi::setRect(const int &x, const int &y, const int &w, const int &h)
{
Rect = QRectF(x,y,w,h);
mBoundingRect.setX(Rect.x() - Rect.width()/2);
mBoundingRect.setY(Rect.y() - Rect.height()/2);
mBoundingRect.setWidth(Rect.width());
mBoundingRect.setHeight(Rect.height());
qDebug()<<"BoundingRect:"<<mBoundingRect;
update();
qDebug()<<"setRect";
}
歡迎各路大神指導討論
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124499.html
標籤:工具平臺和程序庫
上一篇:求解:qt執行緒發信號無回應
