I can paint using QCanvas or QPainter with no problems but when the Widget gets resized my overloaded drawContents method doesn't work the way I want it to:

Using QPainter it only paints to the difference area of the picture and messes everything up. I have to change windows/cover the widget by something else to get it repainted correctly. I already tried setWindow, setViewport, drawArea, update, setAllChanged and setClipping but nothing helps. I also tried to create a workaround by hiding and then showing the widget again. Unfortunately I was unable to get rid of the infinite loop I created by that.

Using QCanvas it repaints everything correctly - but I didn't find any method that clears all QCanvasItems linked to it, so it paints more and more and more each time the method gets called.

The goal is to draw a chart. I am not allowed to use any external program, KDE classes, Qt>3.3.4 or similar, so I am stuck with QCanvas and QPainter. The chart should be able to be completely repainted (scale!) if the size gets changed, so I can't use QCanvas without a method to get rid of everything. I also tried to create a new instance of QCanvas and link it to QCanvasView but unfortunately this also creates an infinite loop.

Trolltech's examples and documentation could not help me with that problem. Unfortunately it has to be solved till tomorrow, so it's quite urgent. Please tell me what I am doing wrong - I would be just fine if I could either clear QCanvas' drawing stack, reinstantiate it or being able to paint to the whole space using QPainter.

Thanks for any comments on this!

audiochart.h:
Qt Code:
  1. #ifndef INCLUDED_AUDIOCHART_H
  2. #define INCLUDED_AUDIOCHART_H
  3.  
  4. #include <qcanvas.h>
  5.  
  6. class AudioChart : protected QCanvasView {
  7. Q_OBJECT
  8.  
  9. public:
  10. AudioChart( QWidget *parent, const char *name );
  11. void drawContents ( QPainter * p, int cx, int cy, int cw, int ch );
  12. private:
  13. QCanvas* myCanvas;
  14. int border;
  15. };
  16.  
  17. #endif
To copy to clipboard, switch view to plain text mode 

audiochart.cpp
Qt Code:
  1. #include "audiochart.h"
  2. #include <qpainter.h>
  3. #include "guicommonhelper.h"
  4. #include <iostream>
  5.  
  6. AudioChart::AudioChart( QWidget *parent=0, const char *name=0 ) : QCanvasView::QCanvasView( parent, name ) {
  7. //QLabel* tmp = new QLabel( "implement me", parent );
  8.  
  9. this->myCanvas = new QCanvas( this, "" );
  10. this->setCanvas( this->myCanvas );
  11. this->myCanvas->setBackgroundColor( QColor( 255, 255, 255 ) );
  12.  
  13. this->border = 10;
  14. this->setVScrollBarMode( QScrollView::AlwaysOff );
  15. this->setHScrollBarMode( QScrollView::AlwaysOff );
  16.  
  17. //this->myCanvas->setDoubleBuffering( false );
  18. }
  19.  
  20. void AudioChart::drawContents( QPainter * p, int cx, int cy, int cw, int ch ) {
  21. this->myCanvas->resize(this->visibleWidth(), this->visibleHeight());
  22.  
  23. QPen pen = QPen( white, 0 );
  24. QBrush brush = QBrush( white );
  25.  
  26. p->setClipping( false );
  27. p->setPen( pen );
  28. p->fillRect( 0, 0, this->myCanvas->width(), this->myCanvas->height(), brush );
  29.  
  30. pen = QPen( black, 1 );
  31. QFont font = QFont( "Helvetica [Cronyx]", 8 );
  32. QFontMetrics* fm = new QFontMetrics( font );
  33.  
  34. char tmpString[255];
  35.  
  36. p->setPen( pen );
  37. p->drawLine( 20, 50, 50, 20 );
  38. p->setFont( font );
  39.  
  40. int w, h, x, y;
  41.  
  42. int max = 10;
  43. int offsetXAxis = 10;
  44. int totalWidth = this->myCanvas->width() - 2*this->border - 10 - offsetXAxis;
  45. int totalHeight = this->myCanvas->height() - 2*this->border;
  46.  
  47. std::cout<<"w "<<totalWidth<<" h "<<totalHeight<<std::endl;
  48.  
  49. std::cout<<"-"<<std::endl;
  50. for (int i = 0; i <= max; i++) {
  51. QString string = QString(intToChar(tmpString, i*3823/max));
  52. w = fm->width(string);
  53. h = fm->height();
  54.  
  55. x = totalWidth*i/max + this->border + offsetXAxis;
  56. y = totalHeight + this->border;
  57.  
  58. std::cout<<i<<": x "<<x<<" y "<<y<<std::endl;
  59.  
  60. p->drawText( x, y, string );
  61. //p->drawRect( x, y-h, w, h );
  62. p->drawLine( x+w/2-1, y-h, x+w/2-1, y-h-2 );
  63. }
  64. std::cout<<"-"<<std::endl;
  65.  
  66. p->flush();
  67. this->myCanvas->setAllChanged();
  68. this->update();
  69. }
To copy to clipboard, switch view to plain text mode