【问题标题】:Set a cross in QImage at the position, where mousepressed在 QImage 中鼠标按下的位置设置一个十字
【发布时间】:2011-12-02 10:51:44
【问题描述】:

我有一个 QImage 中显示的图表,如果按下鼠标右键,我想设置一个黄色的十字 (+) 进行测量。

        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 

         }
        }

如果我在 Paintevent(...) 中这样做,我会破坏原始图片。我该怎么做。

附加信息: 图像被索引。

 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 有一个黑色背景,我想用白色画一个十字 -->(这是索引 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);
      }

但我没有看到任何效果!

【问题讨论】:

  • 你是怎么画线的?您应该使用已初始化的 QPainter 以将 QImage 用作绘画设备。
  • 我使用 QPainter 绘制线条,看看代码的变化。
  • 您是否使用QGraphicsView 来显示图像?
  • 如果我点击我得到错误:QPainter::begin: 不能用 QImage::Format_Indexed8 格式在图像上绘画 QPainter::begin: 不能用 QImage::Format_Indexed8 格式在图像上绘画QPainter::setPen: Painter 未激活 QPainter::setBrush: Painter 未激活

标签: qt qt4 qt4.6 qt4.7


【解决方案1】:

从您的 cmets 中,您无法使用 Format_Indexed8 在 QImage 上绘画。

来自 QImage 文档:

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

选择不同的格式,例如 QImage::Format_ARGB32_Premultiplied,一切都会正常。

【讨论】:

  • QImage::Format_ARGB32 没有帮助
  • QImage::Format_ARGB32_Premultiplied 是枚举,而不是非预乘的枚举。
【解决方案2】:

另一个快速而肮脏的替代方法是简单地设置图像数据中的值。

你将不得不做更多的工作 - 因为没有行命令,请参阅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);
}

【讨论】:

  • 如果图像被索引,那么你需要设置你想要交叉的任何颜色的索引 - 只是一个数字 0-255
  • 图像被索引 my_image.setColorCount(33); for(int i = 0;i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多