zoukankan      html  css  js  c++  java
  • Qt实现九宫图类控件

    <1>. 头文件(类声明)

    class CPreviewWidge : public QWidget
    {
        Q_OBJECT
    public:
        CPreviewWidge( const QString &strPath, QWidget *parent = 0);
        ~CPreviewWidge();
    
        QSize sizeHint()const;
    
    protected:
        void paintEvent(QPaintEvent *e);    //九宫图重绘函数
    
    private:
        QImage    m_imgLTop;           //九宫图图片左上
        QImage    m_imgLCur;           //九宫图图片左中
        QImage    m_imgLBut;           //九宫图图片左下
    
        QImage    m_imgCTop;           //九宫图图片中上
        QImage    m_imgCCur;           //九宫图图片中中
        QImage    m_imgCBut;           //九宫图图片中下
    
        QImage    m_imgRTop;           //九宫图图片右上
        QImage    m_imgRCur;           //九宫图图片右中
        QImage    m_imgRBut;           //九宫图图片右下
    };


    <2>. cpp文件(类定义)

    CPreviewWidge::CPreviewWidge( const QString &strPath, QWidget *parent )    //传参:九宫图中上图片路径和控件父类对象
    : QWidget( parent )
    {
        int iLeft = strPath.lastIndexOf( "_" );
        QString strLeftPath = strPath.left( iLeft );
    
        m_imgLTop = QImage( strLeftPath + "_ltop.png" );
        m_imgLCur = QImage( strLeftPath + "_lcur.png" );
        m_imgLBut = QImage( strLeftPath + "_lbot.png" );
    
        m_imgCTop = QImage( strLeftPath + "_ctop.png" );
        m_imgCCur = QImage( strLeftPath + "_ccur.png" );
        m_imgCBut = QImage( strLeftPath + "_cbot.png" );
    
        m_imgRTop = QImage( strLeftPath + "_rtop.png" );
        m_imgRCur = QImage( strLeftPath + "_rcur.png" );
        m_imgRBut = QImage( strLeftPath + "_rbot.png" );
    }
    
    CPreviewWidge::~CPreviewWidge()
    {
    }
    
    QSize CPreviewWidge::sizeHint() const
    {
        int iHeight = m_imgLTop.height() + m_imgLCur.height() + m_imgLBut.height();
        int iWidth = m_imgLTop.width() + m_imgCTop.width() + m_imgRTop.width();
    
        return QSize( iWidth, iHeight );
    }
    
    void CPreviewWidge::paintEvent ( QPaintEvent* e )
    {
        QWidget::paintEvent( e );
    
        QPainter painter( this );
        QRect r = rect();
    
        painter.drawImage( r.topLeft().x(), r.topLeft().y(), m_imgLTop );
        painter.drawImage( r.topRight().x() - m_imgRTop.width(), r.topRight().y(), m_imgRTop );
        painter.drawImage( r.bottomLeft().x(), r.bottomLeft().y() - m_imgLBut.height(), m_imgLBut );
        painter.drawImage( r.bottomRight().x() - m_imgRBut.width(), r.bottomRight().y() - m_imgRBut.height(), m_imgRBut );
    
        QRect rectLCut( r.topLeft().x(), r.topLeft().y() + m_imgLTop.height(),
                    m_imgLCur.width(), ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
                    painter.drawImage( rectLCut, m_imgLCur );
    
        QRect rectRCut( r.topRight().x() - m_imgRTop.width(), r.topRight().y() + m_imgRTop.height(),
                    m_imgRCur.width(), ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
                    painter.drawImage( rectRCut, m_imgRCur );
    
        QRect rectCTop( r.topLeft().x() + m_imgLTop.width(), r.topLeft().y(),
                    ( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(), m_imgCTop.height() );
                    painter.drawImage( rectCTop, m_imgCTop );
    
        QRect rectCBot( r.bottomLeft().x() + m_imgLBut.width(), r.bottomLeft().y() - m_imgLBut.height(),
                    ( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(), m_imgCBut.height() );
                    painter.drawImage( rectCBot, m_imgCBut );
    
        QRect rectCCur( r.topLeft().x() + m_imgLTop.width(), r.topLeft().y() + m_imgLTop.height(),
                    ( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(),
                    ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
                    painter.drawImage( rectCCur, m_imgCCur );
    }
  • 相关阅读:
    Python 3学习 ——目录 Json Pickle
    Python 3 学习——函数扩展and迭代器生成器装饰器
    Python 3 学习——深浅拷贝以及函数
    Python 3 学习的第七小节——Linux-Ubuntu
    Python 3 —— 文件操作
    Python 3 学习的第五小节——字符编码与三级菜单实践
    关于PHP代码复用‘traits’的一段代码
    一个将对象彻底冻结的函数
    详解vue-cli脚手架项目-package.json
    关于element-ui日期选择器disabledDate使用心得
  • 原文地址:https://www.cnblogs.com/sz-leez/p/4090683.html
Copyright © 2011-2022 走看看