最近项目需要,需要利用QProgressBar显示液位高度,标准液位高度可动态改变,并显示数值。QT中的QProgressBar不能满足以上要求,需对其进行自定义。修改的方法是新类CProgressBar继承于QProgressBar,然后重写其paintenvent函数。以上是主要代码:
void CProgressBar::paintEvent(QPaintEvent *event)
{
QPainter p(this);
const int leftoffset = 15;
const int rightoffset = 30;
const int lineLength = 6;
const int interval = 3;
QRect rect = this->rect();
const double k = (double)(value() - minimum()) / (maximum()-minimum());
int x = (int)(rect.height() * k);
QRect progressbar