Hi to all, I'm writing an audio editor. I have to draw the waveform.
The paint event of my wavewidget is so done:

Qt Code:
  1. /***************************************************************/
  2. /* Paint event */
  3. /***************************************************************/
  4. void WaveWidget::paintEvent( QPaintEvent* pe )
  5. {
  6. // I call updateWave only if necessary if not I draw what is in cache
  7. if( m_waveCachePixmap.isNull() )
  8. {
  9. updateWave();
  10. }
  11.  
  12. QPainter p( this );
  13. p.setRenderHint( QPainter::Antialiasing );
  14. p.drawPixmap( 0, 0, m_waveCachePixmap );
  15. }
To copy to clipboard, switch view to plain text mode 

where the updateWave() is so done:

Qt Code:
  1. /************************************************************************/
  2. /* updateWave */
  3. /* call this only when the wave is changed, */
  4. /************************************************************************/
  5. void WaveWidget::updateWave()
  6. {
  7. int h = height();
  8. int w = width();
  9.  
  10. // I want another width and height here.
  11. // Also, you might to clear the cache at resize events
  12. QPixmap temp = QPixmap( w, h );
  13.  
  14. QPainter p( &temp );
  15. p.fillRect( temp.rect(), Qt::white );
  16. p.setRenderHint( QPainter::Antialiasing );
  17.  
  18.  
  19. QPen pen( Qt::blue, 1 ); // blue solid line, 1 pixels wide
  20. p.setPen( pen );
  21.  
  22. // Draw the waveform or no data thingie
  23. if( m_wave )
  24. {
  25. unsigned int numSamples = m_wave->getNumSamples();
  26. int channels = m_wave->getNumChannels();
  27. FMOD_SOUND_TYPE soundType = m_wave->getSoundType();
  28. FMOD_SOUND_FORMAT soundFormat = m_wave->getSoundFormat();
  29.  
  30. int sampleSize = channels * ( soundFormat == PCM8 ) ? 1 : 2;
  31.  
  32. //char* stream = sound->stream;
  33. void* soundStream = m_wave->getSoundStream();
  34. int maxHeight = (h / 2) / channels;
  35. int increment = (h / 2);
  36.  
  37. switch( soundFormat )
  38. {
  39. case PCM8: //8 bits mono
  40. {
  41. qDebug("PCM8 sound file");
  42. char* audio = reinterpret_cast<char*>(soundStream);
  43. for( unsigned int i = 0; i < numSamples ; i++ )
  44. {
  45. char* current = (audio) + (i * sampleSize);
  46. char audio = *current;
  47. float reproc = audio / (float)(1 << 8);
  48. //DrawLine( i, 0, i, height * reproc );
  49. //QLine line( x, startY, x, startY + Y);
  50. //p.drawLine(line);
  51. //startY += increment;
  52. }
  53. break;
  54. }
  55. case PCM16: //16 bits stereo (LRLRLRLR...LRLR)
  56. {
  57. qDebug("PCM16 sound file");
  58. short* audio = reinterpret_cast<short*>(soundStream);
  59. for( unsigned int i = 0; i < numSamples; i++ )
  60. {
  61. int startY = maxHeight;
  62. for( int k = 0; k < channels; k++ )
  63. {
  64. //short* value1 = reinterpret_cast<short*>(stream + k * 2);
  65. //short* value1 = audio + k * 2;
  66. short* value1 = audio + k;
  67. int x = i / ( numSamples / w);//?
  68. int y = *value1 * maxHeight / 0x0000FFFF * 2;
  69. QLine line( x, startY, x, startY + y );
  70. //std::cout << "Drawing at x: " << x << "startY: " << startY << "y: " << y << "\n";
  71. p.drawLine(line);
  72. startY += increment;
  73. }
  74. //std::cout << audio[i] << "\n";
  75. audio += sampleSize;
  76. }
  77. break;
  78. }
  79.  
  80. default:
  81. {
  82. qDebug("Undefined sound type");
  83. break;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. // Inform the user that no file has been loaded - perhaps using a
  90. // no-data icon or something
  91. qDebug("No data");
  92. }
  93.  
  94. m_waveCachePixmap = temp;
  95. }
To copy to clipboard, switch view to plain text mode 

I have some problems:
1) It doesn't draw nothing
2) I get the follow strange errors that with my poor experience in Qt I can not solve:
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: painter must be active to set rendering hints
QPainter::setPen: Painter not active
3) The step of drawing ( that doesn't work ) blok the application.

I hope to get help.
Best Regards