RubyQT QGraphicsview scale images to fit window
- by shadowskill
I'm trying to make an image scale inside of a QGraphicsView and having zero success. I did find this C++ question with what seemed to be the same problem:http://stackoverflow.com/questions/2489754/qgraphicsview-scrolling-and-image-scaling-cropping
I borrowed the approach of overriding the drawback/foreground. However when I run the script I'm greeted with a method_missing error:
method_missing: undefined method
drawPixmap for #<Qt::Painter:0x7ff11eb9b088>
QPaintDevice: Cannot destroy paint device that is being painted
What should I be doing to get the QGraphicsView to behave the way I want?
class Imageview < Qt::GraphicsView
attr_reader :pixmap
def initialize(parent=nil)
# @parent=parent
super()
@pixmap=pixmap
Qt::MetaObject.connectSlotsByName(self)
end
def loadimage(pixmap)
@pixmap=pixmap
end
def drawForeground(painter, rect)
#size=event.size()
#item= self.items()[0] #Qt::GraphicsPixmapItem.new
#pixmap=@pixmap
#painter=Qt::Painter.new
[email protected](Qt::Size.new(rect.width,rect.height),Qt::KeepAspectRatio, Qt::SmoothTransformation)
# painter.begin(self)
painter.drawPixmap(rect,sized,Qt::Rect.new(0.0, 0.0, sized.width,sized.height))
#painter.end()
# item.setPixmap(sized)
#painter.end
self.centerOn(1.0,1.0)
#item.scale()
# item.setTransformationMode(Qt::FastTransformation)
# self.fitInView(item)
end
def updateSceneRect
painter=Qt::Painter.new()
drawForeground(painter,self.sceneRect)
end
end
app=Qt::Application.new(ARGV)
#grview=Imageview.new
pic=Qt::Pixmap.new('pic')
grview=Imageview.new()
grview.loadimage(pic)
scene=Qt::GraphicsScene.new
#scene.addPixmap(pic)
grview.setScene(scene)
#grview.fitInView(scene)
grview.renderHint=Qt::Painter::Antialiasing | Qt::Painter::SmoothPixmapTransform
grview.backgroundBrush = Qt::Brush.new(Qt::Color.new(230, 200, 167))
grview.show
app.exec