之前的实现有一点不让我满意, 就是无法根据layout自动变化大小, 在写UI时需要迁就该Label大小, 不太方便.
整理后的柱状条主要通过在paintEvent中修正QLabel大小解决不能放入layout的问题
不太会解释, 主要还是来看代码吧
pillarbox.h
- /**
- *=====================================================================
- * Copyright (c) Power Star, Inc. All rights reserved.
- *---------------------------------------------------------------------
- * File Name: pillarbox.h
- *
- * Description:
- * draw pillar with rectangle background
- *
- * History:
- * 2012.04.22 ryan.wang Initial version
- *
- *=====================================================================
- **/
- #ifndef _pillarbox_h_
- #define _pillarbox_h_
- #include <QWidget>
- #include <QLabel>
- #include <QEvent>
- #include <QPaintEvent>
- class PillarBox : public QWidget
- {
- Q_OBJECT
- public:
- PillarBox( QWidget *parent );
- ~PillarBox();
- void resizePillar( int width=0,int height=0 );
- void changePartsDivided( int parts );
- void updatepillar( int val, QColor clr=Qt::green );
- protected:
- virtual void paintEvent(QPaintEvent* event); // 这是重载QWidget的方法
- private:
- void setfgcolor( QColor clr );
- void initialize();
- QLabel *labbg;
- QLabel *labfg;
- int parts_divided;
- int m_value;
- };
- #endif /* _pillarbox_h_ */
pillarbox.cpp
- /**
- *=====================================================================
- * Copyright (c) Power Star, Inc. All rights reserved.
- *---------------------------------------------------------------------
- * File Name: pillarbox.cpp
- *
- * Description:
- * draw pillar with rectangle background
- *
- * History:
- * 2012.04.22 ryan.wang Initial version
- *
- *=====================================================================
- **/
- #ifndef _pillarbox_cpp_
- #define _pillarbox_cpp_
- #include "pillarbox.h"
- #include <QDebug>
- //构造函数
- PillarBox::PillarBox( QWidget *parent ): QWidget(parent)
- {
- m_value = 0;
- initialize();
- }
- //虚构函数
- PillarBox::~PillarBox()
- {
- if ( labbg )
- delete labbg;
- if ( labfg )
- delete labfg;
- }
- //初始化,创建两个label
- void PillarBox::initialize()
- {
- labbg = NULL;
- labfg = NULL;
-
- //label background, that with frame box
- labbg = new QLabel(this);
- labbg->setAlignment( Qt::AlignHCenter|Qt::AlignTop );
- labbg->setFrameShape( QFrame::Box );
- //label foreground, that without frame box but with color filled
- labfg = new QLabel(this);
- labfg->setAutoFillBackground( true );
- //default, the pillar be divided into 100 parts
- parts_divided = 100;
- //default, set the pillar width with 20 pixels
- resizePillar(20);
- //default, set the pillar color with Qt::green
- setfgcolor( Qt::green );
- }
- //改变柱状条尺寸,即改变labbg尺寸
- void PillarBox::resizePillar( int width,int height )
- {
- if( 0 != width )
- labbg->setFixedWidth(width);
-
- if( 0 != height )
- labbg->setFixedHeight( height );
- updatepillar(m_value);
- }
- void PillarBox::changePartsDivided( int parts )
- {
- parts_divided = parts;
- }
- void PillarBox::updatepillar( int val, QColor clr )
- {
- int new_y,new_h; //new height & y_position for labfg
- int height,width;
-
- if( val > parts_divided )
- val = parts_divided;
- m_value = val;
- //if( this->width() > labbg->sizeHint().width() )
- // width = labbg->sizeHint().width();
- //else
- width = this->width()-1;
- //if( this->height() > labbg->sizeHint().height() )
- // height = labbg->sizeHint().height();
- // else
- height = this->height();
- //labbg->setGeometry(QRect(0,0,this->width(),this->height()));
- labbg->resize( width, height );
-
- if( val )
- labbg->setText(QString::number(val));
- else
- labbg->setText(QString(""));
- new_h = val*height/parts_divided;
- new_y = height - new_h;
- labfg->setGeometry( QRect(1,new_y,width-2,new_h-1) );
- //qDebug() << "pillarbox size:";
- //qDebug() << " this:" << this->width() << "," << this->height();
- //qDebug() << " bg hint" << labbg->sizeHint().width() << labbg->sizeHint().height();
- //qDebug() << " fg cur:" << width << height;
- setfgcolor( clr );
- }
- void PillarBox::setfgcolor( QColor clr )
- {
- QPalette palette;
- //QColor clr = Qt::green;
- palette = labfg->palette();
- // clr = QColor::fromRgb(240,240,240);//QColorDialog::getColor(Qt::green, this);
- palette.setColor(labfg->backgroundRole(), clr);
- palette.setColor(labfg->foregroundRole(), clr);
- labfg->setPalette(palette);
- }
- void PillarBox::paintEvent(QPaintEvent* event)
- {
- //qDebug() << "paintEvent" << event->rect();
- resizePillar(event->rect().width(),event->rect().height());
- }
- #endif /* _pillarbox_cpp_ */
阅读(3228) | 评论(0) | 转发(0) |