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

Nothing is impossible

文章分类

全部博文(32)

文章存档

2013年(4)

2012年(13)

2011年(6)

2008年(9)

分类: C/C++

2012-09-21 11:00:06


之前的实现有一点不让我满意, 就是无法根据layout自动变化大小, 在写UI时需要迁就该Label大小, 不太方便.
整理后的柱状条主要通过在paintEvent中修正QLabel大小解决不能放入layout的问题
不太会解释, 主要还是来看代码吧

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 ryan.wang Initial version
  12.  *
  13.  *=====================================================================
  14. **/
  15. #ifndef _pillarbox_h_
  16. #define _pillarbox_h_
  17. #include <QWidget>
  18. #include <QLabel>
  19. #include <QEvent>
  20. #include <QPaintEvent>

  21. class PillarBox : public QWidget
  22. {
  23.     Q_OBJECT
  24. public:
  25.     PillarBox( QWidget *parent );
  26.     ~PillarBox();

  27.     void resizePillar( int width=0,int height=0 );
  28.     void changePartsDivided( int parts );
  29.     void updatepillar( int val, QColor clr=Qt::green );

  30. protected:
  31.     virtual void paintEvent(QPaintEvent* event); // 这是重载QWidget的方法

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

  35.     QLabel *labbg;
  36.     QLabel *labfg;
  37.     int parts_divided;
  38.     int m_value;
  39. };

  40. #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 ryan.wang Initial version
  12.  *
  13.  *=====================================================================
  14. **/
  15. #ifndef _pillarbox_cpp_
  16. #define _pillarbox_cpp_
  17. #include "pillarbox.h"
  18. #include <QDebug>

  19. //构造函数
  20. PillarBox::PillarBox( QWidget *parent ): QWidget(parent)
  21. {
  22.     m_value = 0;
  23.     initialize();
  24. }

  25. //虚构函数
  26. PillarBox::~PillarBox()
  27. {
  28.     if ( labbg )
  29.      delete labbg;
  30.     if ( labfg )
  31.      delete labfg;
  32. }

  33. //初始化,创建两个label
  34. void PillarBox::initialize()
  35. {
  36.     labbg = NULL;
  37.     labfg = NULL;
  38.     
  39.     //label background, that with frame box
  40.     labbg = new QLabel(this);

  41.     labbg->setAlignment( Qt::AlignHCenter|Qt::AlignTop );
  42.     labbg->setFrameShape( QFrame::Box );

  43.     //label foreground, that without frame box but with color filled
  44.     labfg = new QLabel(this);
  45.     labfg->setAutoFillBackground( true );

  46.     //default, the pillar be divided into 100 parts
  47.     parts_divided = 100;

  48.     //default, set the pillar width with 20 pixels
  49.     resizePillar(20);

  50.     //default, set the pillar color with Qt::green
  51.     setfgcolor( Qt::green );
  52. }

  53. //改变柱状条尺寸,即改变labbg尺寸
  54. void PillarBox::resizePillar( int width,int height )
  55. {
  56.     if( 0 != width )
  57.         labbg->setFixedWidth(width);
  58.     
  59.     if( 0 != height )
  60.      labbg->setFixedHeight( height );

  61.     updatepillar(m_value);
  62. }

  63. void PillarBox::changePartsDivided( int parts )
  64. {
  65.     parts_divided = parts;
  66. }
  67. void PillarBox::updatepillar( int val, QColor clr )
  68. {
  69.     int new_y,new_h; //new height & y_position for labfg
  70.     int height,width;
  71.     
  72.     if( val > parts_divided )
  73.         val = parts_divided;

  74.     m_value = val;

  75.     //if( this->width() > labbg->sizeHint().width() )
  76.     //    width = labbg->sizeHint().width();
  77.     //else
  78.         width = this->width()-1;

  79.     //if( this->height() > labbg->sizeHint().height() )
  80.     // height = labbg->sizeHint().height();
  81.     // else
  82.     height = this->height();
  83.     //labbg->setGeometry(QRect(0,0,this->width(),this->height()));
  84.     labbg->resize( width, height );
  85.     
  86.     if( val )
  87.         labbg->setText(QString::number(val));
  88.     else
  89.         labbg->setText(QString(""));

  90.     new_h = val*height/parts_divided;
  91.     new_y = height - new_h;
  92.     labfg->setGeometry( QRect(1,new_y,width-2,new_h-1) );

  93.     //qDebug() << "pillarbox size:";
  94.     //qDebug() << " this:" << this->width() << "," << this->height();
  95.     //qDebug() << " bg hint" << labbg->sizeHint().width() << labbg->sizeHint().height();
  96.     //qDebug() << " fg cur:" << width << height;
  97.     setfgcolor( clr );

  98. }

  99. void PillarBox::setfgcolor( QColor clr )
  100. {
  101.     QPalette palette;
  102.     //QColor clr = Qt::green;

  103.     palette = labfg->palette();
  104.     // clr = QColor::fromRgb(240,240,240);//QColorDialog::getColor(Qt::green, this);
  105.     palette.setColor(labfg->backgroundRole(), clr);
  106.     palette.setColor(labfg->foregroundRole(), clr);
  107.     labfg->setPalette(palette);
  108. }

  109. void PillarBox::paintEvent(QPaintEvent* event)
  110. {
  111.     //qDebug() << "paintEvent" << event->rect();
  112.     resizePillar(event->rect().width(),event->rect().height());
  113. }


  114. #endif /* _pillarbox_cpp_ */


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