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

  1. class CPreviewWidge : public QWidget
  2. {
  3. Q_OBJECT
  4. public:
  5. CPreviewWidge( const QString &strPath, QWidget *parent = );
  6. ~CPreviewWidge();
  7.  
  8. QSize sizeHint()const;
  9.  
  10. protected:
  11. void paintEvent(QPaintEvent *e); //九宫图重绘函数
  12.  
  13. private:
  14. QImage m_imgLTop;        //九宫图图片左上
  15. QImage m_imgLCur;        //九宫图图片左中
  16. QImage m_imgLBut;        //九宫图图片左下
  17.  
  18. QImage m_imgCTop;        //九宫图图片中上
  19. QImage m_imgCCur;        //九宫图图片中中
  20. QImage m_imgCBut;        //九宫图图片中下
  21.  
  22. QImage m_imgRTop;        //九宫图图片右上
  23. QImage m_imgRCur;        //九宫图图片右中
  24. QImage m_imgRBut;        //九宫图图片右下
  25. };

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

  1. CPreviewWidge::CPreviewWidge( const QString &strPath, QWidget *parent ) //传参:九宫图中上图片路径和控件父类对象
  2. : QWidget( parent )
  3. {
  4. int iLeft = strPath.lastIndexOf( "_" );
  5. QString strLeftPath = strPath.left( iLeft );
  6.  
  7. m_imgLTop = QImage( strLeftPath + "_ltop.png" );
  8. m_imgLCur = QImage( strLeftPath + "_lcur.png" );
  9. m_imgLBut = QImage( strLeftPath + "_lbot.png" );
  10.  
  11. m_imgCTop = QImage( strLeftPath + "_ctop.png" );
  12. m_imgCCur = QImage( strLeftPath + "_ccur.png" );
  13. m_imgCBut = QImage( strLeftPath + "_cbot.png" );
  14.  
  15. m_imgRTop = QImage( strLeftPath + "_rtop.png" );
  16. m_imgRCur = QImage( strLeftPath + "_rcur.png" );
  17. m_imgRBut = QImage( strLeftPath + "_rbot.png" );
  18. }
  19.  
  20. CPreviewWidge::~CPreviewWidge()
  21. {
  22. }
  23.  
  24. QSize CPreviewWidge::sizeHint() const
  25. {
  26. int iHeight = m_imgLTop.height() + m_imgLCur.height() + m_imgLBut.height();
  27. int iWidth = m_imgLTop.width() + m_imgCTop.width() + m_imgRTop.width();
  28.  
  29. return QSize( iWidth, iHeight );
  30. }
  31.  
  32. void CPreviewWidge::paintEvent ( QPaintEvent* e )
  33. {
  34. QWidget::paintEvent( e );
  35.  
  36. QPainter painter( this );
  37. QRect r = rect();
  38.  
  39. painter.drawImage( r.topLeft().x(), r.topLeft().y(), m_imgLTop );
  40. painter.drawImage( r.topRight().x() - m_imgRTop.width(), r.topRight().y(), m_imgRTop );
  41. painter.drawImage( r.bottomLeft().x(), r.bottomLeft().y() - m_imgLBut.height(), m_imgLBut );
  42. painter.drawImage( r.bottomRight().x() - m_imgRBut.width(), r.bottomRight().y() - m_imgRBut.height(), m_imgRBut );
  43.  
  44. QRect rectLCut( r.topLeft().x(), r.topLeft().y() + m_imgLTop.height(),
  45. m_imgLCur.width(), ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
  46. painter.drawImage( rectLCut, m_imgLCur );
  47.  
  48. QRect rectRCut( r.topRight().x() - m_imgRTop.width(), r.topRight().y() + m_imgRTop.height(),
  49. m_imgRCur.width(), ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
  50. painter.drawImage( rectRCut, m_imgRCur );
  51.  
  52. QRect rectCTop( r.topLeft().x() + m_imgLTop.width(), r.topLeft().y(),
  53. ( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(), m_imgCTop.height() );
  54. painter.drawImage( rectCTop, m_imgCTop );
  55.  
  56. QRect rectCBot( r.bottomLeft().x() + m_imgLBut.width(), r.bottomLeft().y() - m_imgLBut.height(),
  57. ( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(), m_imgCBut.height() );
  58. painter.drawImage( rectCBot, m_imgCBut );
  59.  
  60. QRect rectCCur( r.topLeft().x() + m_imgLTop.width(), r.topLeft().y() + m_imgLTop.height(),
  61. ( r.topRight().x() - r.topLeft().x() ) - m_imgLTop.width() - m_imgRTop.width(),
  62. ( r.bottomLeft().y() - r.topLeft().y() ) - m_imgLTop.height() - m_imgLBut.height() );
  63. painter.drawImage( rectCCur, m_imgCCur );
  64. }

Qt实现九宫图类控件的更多相关文章

  1. Qt 界面使用自己定义控件 &quot;提升为&quot;

    1.效果图 我做了一个很easy的样例,一个能够显示颜色的QLabel,边上有个button,点击,跳出颜色选取的Dialog,然后选择一个颜色.这个QLabel会变成什么颜色. 2.ColorLab ...

  2. Android RecyclerView滚动类控件修改、去掉滑动边界的阴影效果

    前言 滚动类控件,大家都用的很多,如 RecyclerView.NestedSrollView.... 下面以recyclerView为例讲解,其他滚动控件也同理. RecyclerView 滚动列表 ...

  3. PyQt(Python+Qt)学习随笔:布局控件layout的layoutSizeConstraint属性不起作用的问题解决办法

    在<PyQt(Python+Qt)学习随笔:布局控件layout的layoutSizeConstraint属性>中介绍layout的layoutSizeConstraint属性后,反复测试 ...

  4. Android一个炫酷的树状图组织架构图开源控件实现过程

    Android一个炫酷的树状图组织架构图开源控件 文章目录 [1 简介] [2 效果展示] [3 使用步骤] [4 实现基本布局流程] [5 实现自由放缩及拖动] [6 实现添加删除及节点动画] [7 ...

  5. kettle系列-[KettleUtil]kettle插件,类似kettle的自定义java类控件

    该kettle插件功能类似kettle现有的定义java类插件,自定java类插件主要是支持在kettle中直接编写java代码实现自定特殊功能,而本控件主要是将自定义代码转移到jar包,就是说自定义 ...

  6. 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

    [源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...

  7. 基于Qt的第三方库和控件

    ====================== 基于Qt的第三方库和控件 ======================     libQxt --------   http://dev.libqxt.o ...

  8. C#控件系列--文本类控件

    C#控件系列--文本类控件         文本类控件主要包含Label.LinkLabel.Button.TextBox以及RichTextBox. Label 功能         Label用来 ...

  9. 八、pyqt5按钮类控件——QPushButton、QRadioButton、QCheckBox

    pyqt5中常用的按钮类控件有QPushButton.QRadioButton.QCheckBox.QToolButton等.这些按钮类的基类都是QAbstracButton类.所以这些类有部分方法是 ...

随机推荐

  1. PYC#1欢乐赛第三题题解

    这里是比赛地址:http://tieba.baidu.com/p/2859693237,果然参赛神牛汇集. 第三题题目大意如下: 已知n条二次函数曲线Si(x)=aix^2+bix+ci(ai> ...

  2. gulp实用配置(2)——中小项目

    上一篇的gulp配置很简单,主要就是为了demo的查看和调试,这一篇则会相对详细一些,包括压缩合并打时间戳等. 在互联网环境比较好的城市,需要多人协作的,大一点的项目应该都用上了模块化(这里主要指co ...

  3. MySQL缓存之Qcache与buffer pool对比

    Q:innodb buffer pool和Qcache的缓存区别? A: 1.Qcacche缓存的是SQL语句及对应的结果集,缓存在内存,最简单的情况是SQL一直不重复,那Qcache的命令率肯定是0 ...

  4. Eclipse 迁移到Android studio

    步骤: 1.安装 android-studio-bundle-143.2915827-windows https://developer.android.com/studio/install.html ...

  5. Android6.0 中appcompat_v7 报错

    更新了AndroidSDK以后 各种错误,新建一个项目会附赠一个appcompat_v7,你只要知道这个是一个兼容包就可以了,具体的特性可以看相关介绍,其实也没啥特别的就是为了兼容低版本的呗, 但是呢 ...

  6. java第二课,java基础2

    关键字:            在java中被赋予了特殊含义的单词,具有特殊用途.   标识符:               由字母,数字,下划线(_),美元符($)组成,不能以数字开头,不能是jav ...

  7. tensorflow bias_add应用

    import tensorflow as tf a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32) b=tf.constant([1,-1],dty ...

  8. 用Collections.synchronizedCollection创建线程安全的集合、列表。。。

    Collection c=Collections.synchronizedCollection(new ArrayList()); List list=Collections.synchronized ...

  9. plsql修改表字段alter

    场景:在生产过程中有时候需要不同的环境中修改表字段,使用sql语句比较方便! 1 演示 --添加字段的语法 alter table tablename add (column datatype [de ...

  10. Tomcat结构(转)

    资料:http://wenku.baidu.com/view/20720e78a26925c52cc5bfd6.html Tomcat系统架构 http://wenku.baidu.com/view/ ...