Results 1 to 2 of 2

Thread: Meter Rendering Question

  1. #1
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Meter Rendering Question

    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

  2. #2
    Join Date
    May 2011
    Posts
    23
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Meter Rendering Question

    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:
    Qt Code:
    1. void your_customwidget::update_my_pixmap ( int cx, int cy )
    2. {
    3. // this is an example ...
    4. const QPalette& pal = QBASECLASSOF_YOUR_WIDGET_MAYBE::palette();
    5. m_pixmap = QPixmap(w, h); // member variable
    6. m_pixmap.fill(pal.window().color());
    7.  
    8. QPainter painter(&m_pixmap);
    9. painter.initFrom(this);
    10.  
    11. ... draw on painter here ....
    12. }
    13.  
    14. // call this as pure virtual function from your base class in its PaintEvent...
    15. void your_customwidget::draw_my_contents ( QPainter *pPainter, const QRect& rect )
    16. {
    17. pPainter->drawPixmap(rect, m_pixmap, rect);
    18. }
    19.  
    20. // now on the base:
    21. void your_custom_base::paintEvent ( QPaintEvent *pPaintEvent )
    22. {
    23. QPainter painter(QBASECLASSOF_YOUR_WIDGET_MAYBE....::viewport());
    24. draw_my_contents(&painter, pPaintEvent->rect());
    25. }
    26.  
    27. // in your_custom_base header file
    28. ...
    29. protected:
    30. virtual void draw_my_contents(QPainter *pPainter, const QRect& rect) = 0;
    31. ...
    To copy to clipboard, switch view to plain text mode 

    That's all,

    Best of luck

    Jean
    Last edited by wysota; 1st June 2011 at 18:42. Reason: missing [code] tags

Similar Threads

  1. Replies: 8
    Last Post: 7th November 2012, 20:20
  2. PDF rendering
    By fober in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2011, 20:12
  3. QGraphicsView/QGraphicsScene rendering question
    By onurozcelik in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2010, 12:23
  4. Font rendering on Mac
    By menab0t in forum Qt Programming
    Replies: 0
    Last Post: 19th May 2009, 13:21
  5. how to display batter power meter
    By qtnewbie in forum Newbie
    Replies: 2
    Last Post: 16th October 2007, 22:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.