[PyQt] How: more images in Graphics View

Hans-Peter Jansen hpj at urpla.net
Wed Sep 30 20:31:32 BST 2009


Am Mittwoch 30 September 2009 schrieb p_tierchen:
> Dear reader,
>
> i recently started to develop an application in python using pyqt4 as my
> first project in python. this app when feature complete should allow to
> annotate photographic images with year, author, occasion, ...
> right now i do some experiments on how to implement certain features. as
> my research in the reference and div. other internet platforms did not
> yield any usable result i would like to ask you.
>
> one of these tests deals with loading and displaying multiple images in
> the program window. is it possible to use QGraphicsScene and
> QGraphicsView for this job? I tried to add images as Items to a Scene.
> but the only thing i can see is the last image i added (even without
> list[-1]). I also tried to use lists of Items, however it is not possible
> to add such list to a Scene.

Well, from a cursory look, you're adding just one QGraphicsPixmapItem() to 
the scene. While I didn't used any QGraphicsView() before, Qt tend to 
attach great importance to parent child relationship, e.g. add  
QGraphicsPixmapItem()s with the view as parent, then this ought to be 
enough to keep your objects alive.

Good luck,
Pete

> The following code is the result of what i got so far:
>
> import sys
> from PyQt4 import QtGui, QtCore
> from set_widget_pos_graphicslayout import Ui_MainWindow
>
> class StartGui (QtGui.QMainWindow):
>     def __init__(self, parent=None):
>       QtGui.QWidget.__init__(self, parent)
>       self.ui = Ui_MainWindow()
>       self.ui.setupUi(self)
>       self.pixmp=QtGui.QPixmap()
>       self.pixitem=QtGui.QGraphicsPixmapItem()
>       self.scene=QtGui.QGraphicsScene()
>       self.grview=QtGui.QGraphicsView(self.ui.centralwidget)
>       self.grali=[]
>
>
> QtCore.QObject.connect(self.ui.loadButton,QtCore.SIGNAL("clicked()"),self
>.addIm)
>
>     def addIm(self):
>       fileName = QtGui.QFileDialog.getOpenFileName(self,
>        "Open Image", "/home/jos/Fotos", "Image Files (*.png *.jpg
> *.bmp)"); if fileName:
>           self.pixmp.load(fileName)
>           klbild=self.pixmp.scaledToHeight(200)
>           self.pixitem.setPixmap(klbild)
>           self.grali.append(self.pixitem)
>           self.scene.addItem(self.grali[-1]) # dirty: no progress, just
> to have a working code using a  list (i did not want to rewrite the code
> to get rid of the list)
>           self.grview.setScene(self.scene)
>           self.grview.show()
>
>
> if __name__ == "__main__":
>     app = QtGui.QApplication(sys.argv)
>     myapp = StartGui()
>     myapp.show()
>     sys.exit(app.exec_())
>
> ------ end code ------
>
> i am looking forward to your advice, many thanks in advance
> kind regards
> Jo




More information about the PyQt mailing list