【Qt自定义控件】

Qt自定义控件

  • [1. Qt自定义按钮](#1. Qt自定义按钮)
  • [2. Qt](#2. Qt)
  • [3. Qt](#3. Qt)
  • [4. Qt](#4. Qt)
  • [5. Qt](#5. Qt)

1. Qt自定义按钮

cpp 复制代码
void CButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    paintBackground(&painter);
    paintIcon(&painter);
    paintTextBackground(&painter);
    paintText(&painter);
}

void CButton::paintBackground(QPainter * painter)
{
    painter->save();
    QPainterPath path;
    QBrush brush;
    brush.setStyle(Qt::NoBrush);
    QPen pen;
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(m_BorderWidth);
    //整体外框也分别会有三种状态
    if (isChecked())
    {
        brush.setColor(m_CheckedBackgroundColor);
        pen.setColor(m_CheckedBorderColor);
    }
    else if(!isEnabled())
    {
        brush.setColor(m_DisabledBackgroundColor);
        pen.setColor(m_DisableBorderColor);
    }
    else
    {
        brush.setColor(m_NormalBackgroundColor);
        pen.setColor(m_NormalBorderColor);
    }
    painter->setBrush(brush);
    QRect rect = QRect(0,0,this->width(),this->height());
    painter->setPen(pen);
    path.addRoundedRect(rect, m_BorderRadius, m_BorderRadius);
    //设置允许裁剪,让圆角更自然
    painter->setClipPath(path);
    painter->setClipping(true);
    painter->drawPath(path);
    painter->restore();
}

void CButton::paintIcon(QPainter *painter)
{
    painter->save();
    int iXpos    = 0;
    int iYpos    = 0;
    QPixmap pixmap;
    if (isChecked())
    {
        pixmap = m_CheckedIconPixmap;
    }
    else if (!isEnabled())
    {
        pixmap = m_DisabledIconPixmap;
    }
    else
    {
        pixmap = m_IconPixmap;
    }
    int offsetWidth = 0;
    int offsetHeight = 0;
    QSize pixmapSize(pixmap.width(), pixmap.height());
    if (pixmap.width() > pixmap.height())
    {
        if (pixmap.width() > m_IconSize.rwidth())
        {
            int temp = m_IconSize.rwidth();
            offsetWidth = pixmap.width() - m_IconSize.rwidth();
            int tem = pixmap.height()-offsetWidth;
            pixmapSize=QSize(temp, tem);
        }
    }
    else if (pixmap.height() > pixmap.width())
    {
        if (pixmap.height() > m_IconSize.height())
        {
            int temp = m_IconSize.rheight();
            offsetHeight = pixmap.height() - m_IconSize.rheight();
            int tem = pixmap.width()-offsetHeight;
            pixmapSize=QSize(tem, temp);
        }
    }
    else
    {
        pixmapSize = QSize(pixmap.width(), pixmap.height());
    }
    switch (m_IconSeat)
    {
        case E_ICON_LEFT:
        {
            iXpos = m_IconMargin + ((m_IconSize.rwidth()- pixmapSize.rwidth()) / 2);
            iYpos = (this->rect().height() - pixmapSize.rheight()) / 2;
            pixmap = pixmap.scaled(pixmapSize, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
            painter->drawPixmap(iXpos, iYpos, pixmapSize.rwidth(), pixmapSize.rheight(), pixmap);
        }
            break;
    }
    painter->restore();
}

void CButton::paintTextBackground(QPainter * painter)
{
    painter->save();

    QBrush brush;
    brush.setStyle(Qt::SolidPattern);

    painter->setRenderHint(QPainter::Antialiasing);
    QPainterPath path;
    switch (m_IconSeat)
    {
        case E_ICON_LEFT:
        {
            int posX = m_IconSize.rwidth()+(2*m_IconMargin);
            int posY = m_TextPixmapMargin ;

            int textPixmapWidth = this->width()-m_IconSize.rwidth()-(2*m_IconMargin)-m_TextPixmapMargin;
            int textPixmapHeight = this->height()-(2*m_TextPixmapMargin);
            m_TextPixmapRect = QRectF(posX, posY, textPixmapWidth, textPixmapHeight);
            path.addRoundedRect(m_TextPixmapRect, m_BorderRadius, m_BorderRadius);
            //设置允许裁剪,让圆角更自然
            painter->setClipPath(path);
            painter->setClipping(true);
        }
            break;
    }
    painter->setPen(Qt::NoPen);

    if (isChecked())
    {
        brush.setColor(m_CheckedBackgroundColor);
    }
    else if(!isEnabled())
    {
        brush.setColor(m_DisabledBackgroundColor);
    }
    else
    {
        brush.setColor(m_NormalBackgroundColor);
    }

    painter->fillPath(path, brush);

    painter->drawPath(path);
    painter->restore();
}

void CButton::paintText(QPainter * painter)
{
    painter->save();

    painter->setFont(QFont(m_TextFont, m_TextSize));//设置字体
    QColor color;
    if (isChecked())
    {
        color = m_CheckedTextColor;
    }
    else if (!isEnabled())
    {
        color = m_DisabledTextColor;
    }
    else
    {
        color = m_TextColor;
    }
    painter->setPen(QPen(color, m_TextWidth, Qt::SolidLine));//设置字体颜色
    painter->drawText(m_TextPixmapRect, Qt::AlignHCenter | Qt::AlignVCenter, m_ButtonText);//画一行文字


    painter->restore();
}

2. Qt

3. Qt

4. Qt

5. Qt

相关推荐
码农客栈15 分钟前
qt QWebSocketServer详解
qt
plmm烟酒僧2 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
Black_Friend2 小时前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC2 小时前
【Qt】QTreeView 和 QStandardItemModel的关系
qt
冷凝女子3 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
苏三有春5 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr5 小时前
C++ QT
java·c++·qt
兆。6 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
徒步僧17 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技18 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt