【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

相关推荐
kaixin_learn_qt_ing7 小时前
【银河麒麟下】 安装达梦数据库 + 使用Qt访问达梦数据库
qt
mengzhi啊7 小时前
缓存类CacheDataManager把临时数据存入json。做成插件·
qt·缓存·json
尘客-追梦8 小时前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt
Quz11 小时前
QML SwipeView:实现一个简单的图片浏览器
qt
Quz11 小时前
QML SwipeView:标签导航 与 Loader 懒加载
qt
神仙别闹15 小时前
基于 QT(C++)开发的地铁换乘系统
数据库·c++·qt
C++ 老炮儿的技术栈18 小时前
西门子 S7 常用存储区(I/Q/M/T/C/DB/PI/PQ)
linux·开发语言·c++·windows·qt·io
j7~21 小时前
【Qt】(篇二)《信号与槽:信号和槽的概述,使用、自定义信号和槽、信号与槽的连接方式、信号和槽的其他说明》---详解
qt·信号·qt5·信号与槽··qt creater
Littlehero_1212 天前
QT自定义控件之电气接线图(源码开源)
开发语言·qt
qq762118222 天前
Ubuntu22.04 Qt 调用shell 脚本启动程序,GLFW 初始化失败
qt