简体   繁体   English

qtruby逐点绘制图片

[英]qtruby draw picture point by point

Hi I'm trying to write BMP reader writer in ruby and now i'm stuck on write it on screen. 嗨,我正在尝试用ruby编写BMP读取器作家,现在我被困在屏幕上。 I have picture stored in pixels array and on every pixel is stored rgb color. 我将图片存储在像素数组中,并且在每个像素上都存储了rgb颜色。 But nothing happens in in window? 但是窗口中什么也没有发生? What I'm doing wrong? 我做错了什么? Or is there any qt object to which i can stored pixel data and simply paint it? 还是有任何我可以存储像素数据并简单绘制的qt对象?

def initialize
    super

    setWindowTitle "Transparent rectangles"

    resize 590, 90
    move 300, 300

    show
end

def paintEvent event

      painter = Qt::Painter.new self
      bmp = BMP::Reader.new("picture.bmp")
      drawPicture(painter,bmp.getPixels())
      painter.end
end


def drawPicture(painter, pixels)

    painter.setPen Qt::NoPen
      0.upto(pixels.length-1) do |i|
        0.upto(pixels[0].length-1) do |j|
          painter.setBrush Qt::Brush.new Qt::Color.new pixels[i][j][2], pixels[i][j][1], pixels[i][j][0], 255
          painter.drawPoint(i,j)
        end
      end
end

QPainter.drawPoint uses the current pen, not the brush. QPainter.drawPoint使用当前的笔,而不是画笔。 Call painter.setPen before each point. 在每个点之前调用painter.setPen

But you would be much better off storing the pixels in a QImage . 但是将像素存储在QImage会更好。 Qt already has support for reading BMP files so there's no need to implement that yourself unless you have a good reason to. Qt已经支持读取BMP文件,因此除非您有充分的理由,否则无需自己实现该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM