15

How can I add or import a picture to a QWidget? I have found a clue. I can add a Label and add a Picture in that label. I need the arguments for the QPicture(). The probable I can use is, QLabel.setPicture(self.QPicture).

NorthCat
  • 9,643
  • 16
  • 47
  • 50
V.Sindhuja
  • 151
  • 1
  • 1
  • 3

3 Answers3

19

Here is some code that does what you and @erelender described:

import os,sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
window.setGeometry(0, 0, 400, 200)

pic = QtGui.QLabel(window)
pic.setGeometry(10, 10, 400, 100)
#use full ABSOLUTE path to the image, not relative
pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/logo.png"))

window.show()
sys.exit(app.exec_())

A generic QWidget does not have setPixmap(). If this approach does not work for you, you can look at making your own custom widget that derives from QWidget and override the paintEvent method to display the image.

swanson
  • 7,377
  • 4
  • 32
  • 34
  • this puts the QLabel ontop of all the widgets I have , how would you put it behind every widget and always fit to width of window if resized? – Ciasto piekarz Nov 04 '13 at 16:16
15

QPicture is not what you want. QPicture records and replays QPainter commands. What you want is QPixmap. Give a filename to the QPixmap constructor, and set this pixmap to the label using QLabel.setPixmap().

An implementation example in python would be:

label = QLabel() 
pixmap = QPixmap('path_to_your_image')
label.setPixmap(pixmap)
Jairo Vadillo
  • 345
  • 1
  • 3
  • 13
erelender
  • 6,175
  • 32
  • 49
0

Use this snippet in your GUI script:

image = QtGui.QLabel(self)
image.setGeometry(50, 40, 250, 250)
pixmap = QtGui.QPixmap("C:\\address\\to\\your_image.png")
image.setPixmap(pixmap)
image.show()