效果显示
说明:
使用两个QLabel叠加,背景用一个带边框(QFrame::Box)的QLabel,前景则用一个不带边框但使用了palette的可调颜色的QLabel
实现代码(未整理)如下:
pillarbox.h
- /**
- *=====================================================================
- * Copyright (c) Power Star, Inc. All rights reserved.
- *---------------------------------------------------------------------
- * File Name: pillarbox.h
- *
- * Description:
- * draw pillar with rectangle background
- *
- * History:
- * 2012.04.22 qspy.wang Initial version
- *
- *=====================================================================
- **/
- #ifndef _pillarbox_h_
- #define _pillarbox_h_
- #include <QWidget>
- #include <QLabel>
- #include <QDebug>
- class PillarBox : public QWidget
- {
- Q_OBJECT
- public:
- PillarBox( QWidget *parent , int max_height, int max_value );
- PillarBox( QWidget *parent , QSize bgsize, int max_value );
- ~PillarBox();
- void setBgSize( QSize size );
- void setBgWidth( int width );
- void setBgHeight( int height );
- void setMaxValue( int max );
- void updatepillar( int val, QColor clr );
- private:
- void setfgcolor( QColor clr );
- QLabel *labbg;
- QLabel *labfg;
- int max_divide;
- };
- #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 qspy.wang Initial version
- *
- *=====================================================================
- **/
- #ifndef _pillarbox_cpp_
- #define _pillarbox_cpp_
- #include "pillarbox.h"
- PillarBox::PillarBox( QWidget *parent , int max_height, int max_value )
- {
- labbg = new QLabel(this);
- labbg->setAlignment( Qt::AlignHCenter|Qt::AlignTop );
- labbg->setFrameShape( QFrame::Box );
- labbg->setFixedHeight( max_height );
- //labbg->setText("99");
- labfg = new QLabel(this);
- labfg->setAutoFillBackground( true );
- max_divide = max_value;
- }
- PillarBox::PillarBox( QWidget *parent , QSize bgsize, int max_value )
- {
- labbg = new QLabel(this);
- labbg->setAlignment( Qt::AlignHCenter|Qt::AlignTop );
- labbg->setFrameShape( QFrame::Box );
- labbg->setFixedSize(bgsize);
- //labbg->setFixedHeight( max_height );
- //labbg->setText("99");
- labfg = new QLabel(this);
- labfg->setAutoFillBackground( true );
- max_divide = max_value;
- }
- PillarBox::~PillarBox()
- {
- if ( labbg )
- delete labbg;
- if ( labfg )
- delete labfg;
- }
- void PillarBox::setBgSize( QSize size )
- {
- labbg->setFixedSize(size);
- }
- void PillarBox::setBgWidth( int width )
- {
- labbg->setFixedWidth(width);
- }
- void PillarBox::setBgHeight( int height )
- {
- labbg->setFixedHeight( height );
- }
- void PillarBox::setMaxValue( int max )
- {
- max_divide = max;
- }
- void PillarBox::updatepillar( int val, QColor clr )
- {
- int new_y,new_h,height,width;
-
- if( val > max_divide )
- val = max_divide;
- if( this->width() > labbg->sizeHint().width() )
- width = labbg->sizeHint().width();
- else
- width = this->width();
- 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-1, height-1 );
- if( val )
- labbg->setText(QString::number(val));
- else
- labbg->setText(QString(""));
- new_h = val*height/max_divide;
- new_y = height - new_h;
- labfg->setGeometry(QRect(1,new_y,width-2,new_h-1));
- 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);
- }
- #endif /* _pillarbox_cpp_ */
阅读(9631) | 评论(1) | 转发(0) |