關於QT的QPainterPath::arcTo 詳解


這個函數文檔的意思就是畫弧,看了文檔也不太明白,自己做了demo終於明白了意思

移動到圓心,畫180度半圓

void TestArcTo::paintEvent(QPaintEvent *)
{
QPoint startPt(30, 30);
QRect rect(startPt.x(), startPt.y(), 200, 200);
QPainter p(this); 
p.setRenderHint(QPainter::Antialiasing); //抗鋸齒
p.fillRect(rect, QColor(255, 255, 255));

int arcR = rect.width()/2;
int rectSize = rect.width();
QPainterPath path;

path.moveTo(startPt.x() + arcR, startPt.y() + arcR); //先移動到圓心
path.arcTo(rect, 00.0f, 180.0f);      //以0度起點,逆時針畫180度

p.fillPath(path, QBrush(QColor(122, 122, 122)));
}

 

 

移動到圓心,以90度開始畫180度半圓

path.moveTo(startPt.x() + arcR, startPt.y() + arcR); //先移動到圓心
path.arcTo(rect, 90.0f, 180.0f); //以0度起點,逆時針畫180度

 

 

移動到圓心,以190度開始畫180度半圓

path.moveTo(startPt.x() + arcR, startPt.y() + arcR); //先移動到圓心
path.arcTo(rect, 90.0f, 180.0f);      //以0度起點,逆時針畫180度

 

 

 

移動到某個點可以畫弦月

 

幾個點組合

 

矩形區圓角

void TestArcTo::paintEvent(QPaintEvent *)
{
    QRect rect(30, 30, 200, 200);
    QPainter p(this);  
    p.setRenderHint(QPainter::Antialiasing);
    p.fillRect(rect, QColor(255, 255, 0));

    int cornerSize = 50;       //調節圓角的大小
    int arcR = cornerSize/2;
    QPainterPath path;
    path.moveTo(rect.left() + arcR, rect.top());
    path.arcTo(rect.left(), rect.top(), cornerSize, cornerSize, 90.0f, 90.0f);

    path.lineTo(rect.left(), rect.bottom() - arcR);
    path.arcTo(rect.left(), rect.bottom() - cornerSize, cornerSize, cornerSize, 180.0f, 90.0f);

    path.lineTo(rect.right() - arcR, rect.bottom());
    path.arcTo(rect.right() - cornerSize, rect.bottom() - cornerSize, cornerSize, cornerSize, 270.0f, 90.0f);

    path.lineTo(rect.right(), rect.top() + arcR);
    path.arcTo(rect.right() - cornerSize, rect.top(), cornerSize, cornerSize, 0.0f, 90.0f);

    p.fillPath(path, QBrush(QColor(122, 122, 122)));
}

底部和右邊有黃色邊框需要處理,這就需要+1, -1微調了。理解了arcTo函數,都不難處理。

 

最方便的圓角方法

void TestArcTo::paintEvent(QPaintEvent *)
{
    QRect rect(30, 30, 200, 200);
    QPainter p(this);  
    p.setRenderHint(QPainter::Antialiasing);
    p.fillRect(rect, QColor(255, 255, 0));

    p.setPen(Qt::NoPen);
    p.setBrush(QColor(122, 122, 122));
    p.drawRoundedRect(rect, 20, 20);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM