Chinaunix首页 | 论坛 | 博客
  • 博客访问: 421914
  • 博文数量: 32
  • 博客积分: 1843
  • 博客等级: 上尉
  • 技术积分: 634
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-25 14:30
个人简介

Nothing is impossible

文章分类

全部博文(32)

文章存档

2013年(4)

2012年(13)

2011年(6)

2008年(9)

分类: C/C++

2012-04-25 23:43:30

效果显示
说明:
使用两个QLabel叠加,背景用一个带边框(QFrame::Box)的QLabel,前景则用一个不带边框但使用了palette的可调颜色的QLabel

实现代码(未整理)如下:
pillarbox.h

点击(此处)折叠或打开

  1. /**
  2.  *=====================================================================
  3. * Copyright (c) Power Star, Inc. All rights reserved.
  4.  *---------------------------------------------------------------------
  5.  * File Name: pillarbox.h
  6.  *
  7.  * Description:
  8.  * draw pillar with rectangle background
  9.  *
  10.  * History:
  11.  * 2012.04.22 qspy.wang Initial version
  12.  *
  13.  *=====================================================================
  14. **/
  15. #ifndef _pillarbox_h_
  16. #define _pillarbox_h_
  17. #include <QWidget>
  18. #include <QLabel>
  19. #include <QDebug>

  20. class PillarBox : public QWidget
  21. {
  22.     Q_OBJECT
  23. public:
  24.     PillarBox( QWidget *parent , int max_height, int max_value );
  25.     PillarBox( QWidget *parent , QSize bgsize, int max_value );
  26.     ~PillarBox();

  27.     void setBgSize( QSize size );
  28.     void setBgWidth( int width );
  29.     void setBgHeight( int height );
  30.     void setMaxValue( int max );
  31.     void updatepillar( int val, QColor clr );

  32. private:
  33.     void setfgcolor( QColor clr );

  34.     QLabel *labbg;
  35.     QLabel *labfg;
  36.     int max_divide;
  37. };

  38. #endif /* _pillarbox_h_ */

pillarbox.cpp

点击(此处)折叠或打开

  1. /**
  2. *=====================================================================
  3. * Copyright (c) Power Star, Inc. All rights reserved.
  4. *---------------------------------------------------------------------
  5. * File Name: pillarbox.cpp
  6. *
  7. * Description:
  8. * draw pillar with rectangle background
  9. *
  10. * History:
  11. * 2012.04.22 qspy.wang Initial version
  12. *
  13. *=====================================================================
  14. **/
  15. #ifndef _pillarbox_cpp_
  16. #define _pillarbox_cpp_
  17. #include "pillarbox.h"
  18. PillarBox::PillarBox( QWidget *parent , int max_height, int max_value )
  19. {
  20. labbg = new QLabel(this);
  21. labbg->setAlignment( Qt::AlignHCenter|Qt::AlignTop );
  22. labbg->setFrameShape( QFrame::Box );
  23. labbg->setFixedHeight( max_height );
  24. //labbg->setText("99");
  25. labfg = new QLabel(this);
  26. labfg->setAutoFillBackground( true );
  27. max_divide = max_value;
  28. }
  29. PillarBox::PillarBox( QWidget *parent , QSize bgsize, int max_value )
  30. {
  31. labbg = new QLabel(this);
  32. labbg->setAlignment( Qt::AlignHCenter|Qt::AlignTop );
  33. labbg->setFrameShape( QFrame::Box );
  34. labbg->setFixedSize(bgsize);
  35. //labbg->setFixedHeight( max_height );
  36. //labbg->setText("99");
  37. labfg = new QLabel(this);
  38. labfg->setAutoFillBackground( true );
  39. max_divide = max_value;
  40. }
  41. PillarBox::~PillarBox()
  42. {
  43. if ( labbg )
  44. delete labbg;
  45. if ( labfg )
  46. delete labfg;
  47. }
  48. void PillarBox::setBgSize( QSize size )
  49. {
  50. labbg->setFixedSize(size);
  51. }
  52. void PillarBox::setBgWidth( int width )
  53. {
  54. labbg->setFixedWidth(width);
  55. }
  56. void PillarBox::setBgHeight( int height )
  57. {
  58. labbg->setFixedHeight( height );
  59. }
  60. void PillarBox::setMaxValue( int max )
  61. {
  62. max_divide = max;
  63. }
  64. void PillarBox::updatepillar( int val, QColor clr )
  65. {
  66. int new_y,new_h,height,width;
  67. if( val > max_divide )
  68. val = max_divide;
  69. if( this->width() > labbg->sizeHint().width() )
  70. width = labbg->sizeHint().width();
  71. else
  72. width = this->width();
  73. if( this->height() > labbg->sizeHint().height() )
  74. height = labbg->sizeHint().height();
  75. else
  76. height = this->height();
  77. //labbg->setGeometry(QRect(0,0,this->width(),this->height()));
  78. labbg->resize( width-1, height-1 );
  79. if( val )
  80. labbg->setText(QString::number(val));
  81. else
  82. labbg->setText(QString(""));
  83. new_h = val*height/max_divide;
  84. new_y = height - new_h;
  85. labfg->setGeometry(QRect(1,new_y,width-2,new_h-1));
  86. setfgcolor( clr );
  87. }
  88. void PillarBox::setfgcolor( QColor clr )
  89. {
  90. QPalette palette;
  91. //QColor clr = Qt::green;
  92. palette = labfg->palette();
  93. // clr = QColor::fromRgb(240,240,240);//QColorDialog::getColor(Qt::green, this);
  94. palette.setColor(labfg->backgroundRole(), clr);
  95. //palette.setColor(labfg->foregroundRole(), clr);
  96. labfg->setPalette(palette);
  97. }
  98. #endif /* _pillarbox_cpp_ */

阅读(9591) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

煜轩2012-04-26 18:49:42

不错,代码很好啊,不过注释少了一点啊