0

I have a graph shown in a QImage and want to set a cross (+) in yellow colour for measurement, if right mouse button is pressed.

        void foo::mousePressEvent(QMouseEvent *event)
        {
         if (event->button() == Qt::RightButton) {
            QPoint pos = event->pos();
            int x = pos.x();
           int y = pos.y();
          QLine line(x-5,y,x+5,y);
          QLine line(x,y-5,x,y+5);
          QPainter painter(&my_image);
          painter.setPen( Qt::red );
          painter.setBrush( Qt::yellow );
/*
QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
*/


              painter.drawLine(line); //no effect 

         }
        }

if I do it in Paintevent(...), I destroy the original pic. how can I do it.

additional Information: the imag is indexed.

 my_image.setColorCount(33);
    for(int i = 0;i<33;i++)
    {
        my_image.setColor(i,qRgb((unsigned char)palette[i*3], (unsigned char)palette[i*3+1], (unsigned char)palette[i*3+2]));
    }

my_imag has a black-Background and I want to draw a cross in white color --> (this is the index 32)

int color = 32;//_index_value_of_cross_color;

      for (int ix=x-5;ix<x+5;ix++) {
           my_image.setPixel(ix,y,color);
      }

      for (int iy=y-5;iy<y+5;iy++) {
           my_imag.setPixel(x,iy,color);
      }

but I see no effect !

Martin R
  • 71
  • 9
  • 1
    How are you drawing the lines? You should be using a QPainter initialised to use the QImage as a paint device. – cmannett85 Dec 02 '11 at 11:47
  • I draw the Lines using QPainter see changes in code . – Martin R Dec 02 '11 at 11:52
  • Do you use a `QGraphicsView` for displaying the image? – pnezis Dec 02 '11 at 12:09
  • if I click I get the Error: QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format QPainter::setPen: Painter not active QPainter::setBrush: Painter not active – Martin R Dec 02 '11 at 12:44

2 Answers2

2

From your comments, you cannot paint on a QImage with Format_Indexed8.

From the QImage docs:

Warning: Painting on a QImage with the format QImage::Format_Indexed8 is not supported.

Choose a different format like QImage::Format_ARGB32_Premultiplied and things should work.

Trenton Schulz
  • 778
  • 3
  • 8
1

Another quick and dirty alternative is to simply set the values in the image data.

You will have to do a little more work - because there is no line command, see setpixel

int x = pos.x();
int y = pos.y();
int color = _index_value_of_cross_color;

for (int ix=x-5;ix<x+5;ix++) {
     my_image.setPixel(ix,y,color);
}

for (int iy=y-5;iy<y+5;iy++) {
     my_image.setPixel(x,iy,color);
}
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • If the image is indexed then you need to set the index of whichever color you want the cross - just a number 0-255 – Martin Beckett Dec 05 '11 at 16:38
  • the image is indexed my_image.setColorCount(33); for(int i = 0;i<33;i++) my_image.setColor(i,qRgb(r[i], g[i], b[i])); and I set the color to 32 (white). background is black. But I see no effect. – Martin R Dec 06 '11 at 07:27