title: How to create gray scale QImage(QImage::Format_Indexed) without copying memory
body: <p>I'm trying to create QImage that wrap a existing image buffer that is created by OpenCv
  I was considering use following constructor to do this.</p>
  
  <pre><code>QImage::QImage ( const uchar * data, int width, int height, 
                   int bytesPerLine, Format format )
  </code></pre>
  
  <p>so, my code is like </p>
  
  <pre><code>QImage qimage((const uchar*)iplImage-&gt;imageData, 
                iplImage-&gt;width, iplImage-&gt;height, 
                iplImage-&gt;widthStep, 
                QImage::Format_Indexed);     // image buffer not copied!
  qimage.setColorTable(grayScaleColorTable); // color table's item count 256 for grayscale.
                                             // now new image buffer is allocated here.
  </code></pre>
  
  <p>Ok, no memory copy actually was done at the time of calling this ctor. 
  But, here comes my problem. <code>QImage::setColorTable()</code> is non const member function where QImage allocates new image buffer for copying by its internal detach() function. </p>
  
  <p>I found there was Qt3 support for this kind of problem where ctor could accept color table as argument in its ctor, but I've not found any such support in > Qt4. </p>
  
  <p>How can I create gray scale QImage for existing image buffer?</p>
  
  <p>Thanks for in advance</p>
  
  <p>[EDITED]</p>
  
  <p>Thanks to Stephen Chu, I realized that following contstructors create read/write-able QImage object </p>
  
  <pre><code>    QImage ( uchar * data, int width, int height, Format format )
      QImage ( uchar * data, int width, int height, int bytesPerLine, Format format )
  </code></pre>
  
  <p>which even if <code>QImage::setColorTable()</code> is called later right after instantiation, no new buffer is allocated. On the other hand, following constructors receiving 'const'ed data buffer create read-only QImage objects which new buffer is allocated and deep copied from original buffer when any non-const member function like <code>QImage::setColorTable()</code> is called(that I do not want).</p>
  
  <pre><code>    QImage ( const uchar * data, int width, int height, Format format )
      QImage ( const uchar * data, int width, int height, int bytesPerLine, Format format )
  </code></pre>