欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Qt電池電量控件怎么實(shí)現(xiàn)

這篇文章主要講解了“Qt電池電量控件怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Qt電池電量控件怎么實(shí)現(xiàn)”吧!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供達(dá)坂城網(wǎng)站建設(shè)、達(dá)坂城做網(wǎng)站、達(dá)坂城網(wǎng)站設(shè)計(jì)、達(dá)坂城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、達(dá)坂城企業(yè)網(wǎng)站模板建站服務(wù),十年達(dá)坂城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

一、前言

現(xiàn)在這個(gè)時(shí)代,智能手機(jī)不要太流行,滿大街都是,甚至連爺爺奶奶級(jí)別的人都會(huì)用智能手機(jī),本次要寫的控件就是智能手機(jī)中的電池電量表示控件,采用純painter繪制,其實(shí)也可以采用貼圖,我估計(jì)大部分手機(jī)上的都是采用貼圖的形式,貼圖有個(gè)好處就是程序員不用操心,drawimage即可,速度非??臁?至于本控件沒有任何技術(shù)難點(diǎn),就是自動(dòng)計(jì)算當(dāng)前設(shè)置的電量,根據(jù)寬度的比例劃分100個(gè)等分,每個(gè)等分占用多少個(gè)像素,然后電量*該比例就是要繪制的電量的區(qū)域,可以設(shè)置報(bào)警電量,低于該變量整個(gè)電池電量區(qū)域紅色顯示。

主要功能:

  1. 可設(shè)置開關(guān)按鈕的樣式 圓角矩形/內(nèi)圓形/外圓形

  2. 可設(shè)置選中和未選中時(shí)的背景顏色

  3. 可設(shè)置選中和未選中時(shí)的滑塊顏色

  4. 可設(shè)置顯示的文本

  5. 可設(shè)置滑塊離背景的間隔

  6. 可設(shè)置圓角角度

  7. 可設(shè)置是否顯示動(dòng)畫過渡效果

二、代碼思路

void Battery::paintEvent(QPaintEvent *)
{
    //繪制準(zhǔn)備工作,啟用反鋸齒
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //繪制邊框
    drawBorder(&painter);
    //繪制背景
    drawBg(&painter);
    //繪制頭部
    drawHead(&painter);
}

void Battery::drawBorder(QPainter *painter)
{
    painter->save();

    double headWidth = width() / 10;
    double batteryWidth = width() - headWidth;

    //繪制電池邊框
    QPointF topLeft(5, 5);
    QPointF bottomRight(batteryWidth, height() - 5);
    batteryRect = QRectF(topLeft, bottomRight);

    painter->setPen(QPen(borderColorStart, 5));
    painter->setBrush(Qt::NoBrush);
    painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);

    painter->restore();
}

void Battery::drawBg(QPainter *painter)
{
    painter->save();

    QLinearGradient batteryGradient(QPointF(0, 0), QPointF(0, height()));
    if (currentValue <= alarmValue) {
        batteryGradient.setColorAt(0.0, alarmColorStart);
        batteryGradient.setColorAt(1.0, alarmColorEnd);
    } else {
        batteryGradient.setColorAt(0.0, normalColorStart);
        batteryGradient.setColorAt(1.0, normalColorEnd);
    }

    int margin = qMin(width(), height()) / 20;
    double unit = (batteryRect.width() - (margin * 2)) / 100;
    double width = currentValue * unit;
    QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
    QPointF bottomRight(width + margin + 5, batteryRect.bottomRight().y() - margin);
    QRectF rect(topLeft, bottomRight);

    painter->setPen(Qt::NoPen);
    painter->setBrush(batteryGradient);
    painter->drawRoundedRect(rect, bgRadius, bgRadius);

    painter->restore();
}

void Battery::drawHead(QPainter *painter)
{
    painter->save();

    QPointF headRectTopLeft(batteryRect.topRight().x(), height() / 3);
    QPointF headRectBottomRight(width(), height() - height() / 3);
    QRectF headRect(headRectTopLeft, headRectBottomRight);

    QLinearGradient headRectGradient(headRect.topLeft(), headRect.bottomLeft());
    headRectGradient.setColorAt(0.0, borderColorStart);
    headRectGradient.setColorAt(1.0, borderColorEnd);

    painter->setPen(Qt::NoPen);
    painter->setBrush(headRectGradient);
    painter->drawRoundedRect(headRect, headRadius, headRadius);

    painter->restore();
}

三、效果圖

Qt電池電量控件怎么實(shí)現(xiàn)

感謝各位的閱讀,以上就是“Qt電池電量控件怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Qt電池電量控件怎么實(shí)現(xiàn)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

分享文章:Qt電池電量控件怎么實(shí)現(xiàn)
本文地址:http://chinadenli.net/article32/gidipc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計(jì)公司動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、用戶體驗(yàn)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作