【Qt自定义控件】

1. Qt自定义按钮

在这里插入图片描述

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

相关推荐

  1. QT中如何使用定义

    2023-12-06 14:48:03       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 14:48:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 14:48:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 14:48:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 14:48:03       18 阅读

热门阅读

  1. Python edge-tts库全部声音模型一览表

    2023-12-06 14:48:03       25 阅读
  2. 深度学习与深度迁移学习有什么区别?

    2023-12-06 14:48:03       39 阅读
  3. flutter的Overlay详解

    2023-12-06 14:48:03       37 阅读
  4. IDC网络设备监控脚本-FLOW流监控

    2023-12-06 14:48:03       26 阅读
  5. 代码随想录二刷 |队列与栈 |有效的括号

    2023-12-06 14:48:03       42 阅读
  6. ubuntu重启后下无wifi,蓝牙和飞行模式切换问题

    2023-12-06 14:48:03       39 阅读
  7. github可访问但无法clone问题

    2023-12-06 14:48:03       35 阅读
  8. Linux计算机系统参数获取和压力测试

    2023-12-06 14:48:03       37 阅读
  9. Ubuntu22.04LTS配置rsync服务

    2023-12-06 14:48:03       44 阅读
  10. [cmake] --- find_package

    2023-12-06 14:48:03       27 阅读