PDA

View Full Version : Meter Rendering Question



cpsmusic
1st June 2011, 14:59
Hi,

I'm currently working on an audio application that requires a volume meter. I'm going to implement this as a custom widget and override the paintEvent method. I'd like the meter display (basically a coloured rectangle) to change colour depending on the current signal value, changing from green to yellow to orange to red as the meter approaches full scale.

Initially, I was going to calculate the meter's display on the fly using QLinearGradient. While I think this will work, I was thinking that because the colour value of each meter degree is always the same it would be better to create the full meter display in an offscreen buffer, and then copy whatever portion of it corresponds to the current signal value and render that.

Can I use something like QPixmap for rendering the full meter display and then copy parts of it during rendering? Any suggestions on how to implement this?

Cheers,

Chris

jeanremi
1st June 2011, 16:52
Yes you can, use the drawPixmap function from your QWidget in your paint event function when you know that the pixmap is ready to be displayed.
(this technique is also known as local double-buffering)

Best of luck.

Jean

Added after 14 minutes:

I thought I'd leave you a bit more code....

Here:

void your_customwidget::update_my_pixmap ( int cx, int cy )
{
// this is an example ...
const QPalette& pal = QBASECLASSOF_YOUR_WIDGET_MAYBE::palette();
m_pixmap = QPixmap(w, h); // member variable
m_pixmap.fill(pal.window().color());

QPainter painter(&m_pixmap);
painter.initFrom(this);

... draw on painter here ....
}

// call this as pure virtual function from your base class in its PaintEvent...
void your_customwidget::draw_my_contents ( QPainter *pPainter, const QRect& rect )
{
pPainter->drawPixmap(rect, m_pixmap, rect);
}

// now on the base:
void your_custom_base::paintEvent ( QPaintEvent *pPaintEvent )
{
QPainter painter(QBASECLASSOF_YOUR_WIDGET_MAYBE....::viewpo rt());
draw_my_contents(&painter, pPaintEvent->rect());
}

// in your_custom_base header file
...
protected:
virtual void draw_my_contents(QPainter *pPainter, const QRect& rect) = 0;
...

That's all,

Best of luck

Jean