Search code examples
pythonpyqtqpixmap

Insert a Pixmap created in another class


I am trying to create a QMainWindow and add some QDockWidget, QPushButtons and an image in it. I created the image in a different class so i can add some characteristics later.

I need to put the buttons and the image in the left side of the QMainWindow. Something like this:

enter image description here

As you can see, there are some QPushButton there and the QDockWidget with a matplotlib figure in each of them.

The problem comes when i want to add the image. I need it to be located in the lower left corner of the QMainWindow.

This is part of the code:

class MainWindow(QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()

    self.my_widget = QWidget()
    self.setCentralWidget(self.my_widget)

    self.createDockWindows()
    self.createButtons()

  def createDockWindows(self):
    #Here I create two QDockWidget and
    #I add into them a matplotlib figure created in another class

    self.fig1 = mpl_Canvas()    #This is the matplotlib figure`s class
    self.fig2 = mpl_Canvas()

    dock1.setWidget(self.fig1)
    dock2.setWidget(self.fig2)

    self.addDockWidget(Qt.RightDockWidgetArea, dock1)
    self.addDockWidget(Qt.RightDockWidgetArea, dock2)

def createButtons(self):
    #Here i add some buttons  
    layout = QVBoxLayout()

    self.r_btn = QRadioButton()
    self.r_btn.setGeometry(10, 50, 10, 30)
    self.r_btn.setText("RadioButton 1")

    ###HERE IS WHERE I NEED TO ADD THE IMAGE FROM THE CREATEIMAGE CLASS###

    self.image = createImage()

    layout.addWidget(self.r_btn)
    layout.addWidget(self.image)

    self.my_widget.setLayout(layout)

class createImage(QWidget):
  def __init__(self):
    QWidget.__init__(self)

    self.showImage()

  def showImage(self):
    self.label = QLabel()
    self.img = QPixmap("image.jpg")
    self.img_scaled = self.img.scaled(self.label.size(),Qt.KeepAspectRatio)

    self.label.setPixmap(self.img_scaled)

How can i do this? I know that i am doing something wrong but i can`t find the mistake.

Hope you can help me.

-------- EDIT ---------

This is the code with the modifications from @K.Mulier:

class MainWindow(QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()

    self.my_widget = QWidget()
    self.setCentralWidget(self.my_widget)

    self.createDockWindows()
    self.createButtons()

  def createDockWindows(self):
    dock1 = QDockWidget("Osciloscope",self)
    dock1.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)

    dock2 = QDockWidget("Espectrometer", self)
    dock2.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable
        | QDockWidget.DockWidgetClosable)

    dock1.setMinimumSize(800,100)
    dock2.setMinimumSize(800,100)

    self.fig1 = mpl_Canvas()
    self.fig2 = mpl_Canvas()

    dock1.setWidget(self.fig1)
    dock2.setWidget(self.fig2)

    self.addDockWidget(Qt.RightDockWidgetArea, dock1)
    self.addDockWidget(Qt.RightDockWidgetArea, dock2)

  def createButtons(self):
    layout = QVBoxLayout()

    self.r_btn = QRadioButton()
    self.r_btn.setGeometry(10, 50, 10, 30)
    self.r_btn.setText("RadioButton 1")

    self.image = createImage()

    layout.addWidget(self.r_btn)
    layout.addWidget(self.image)

    self.my_widget.setLayout(layout)

class createImage(QWidget):
  def __init__(self):
    super(createImage, self).__init__(parent = None)
    self.mainLayout = QHBoxLayout()
    self.setLayout(self.mainLayout)

    self.showImage()


  def showImage(self):

    picPath = os.getcwd() + "C:\Users\Flosh\Desktop\hello.png"
    print picPath
    picMap = QPixmap(picPath)

    picLabel = QLabel(parent = None)
    picLabel.setGeometry(10,10,800,600)
    picLabel.setPixmap(picMap.scaled(780,580,Qt.KeepAspectRatio))
    self.mainLayout.addWidget(picLabel)

class mpl_canvas(self):
  #A matplotlib figure

EDIT - PROBLEM SOLVED

The problem in the last code is in these lines:

picPath = os.getcwd() + "C:\somedirectory\image.png"
print picPath
picMap = QPixmap(picPath) 

You must replace picPath in picMap = QPixmap(picPath) with the location of the image like this:

picMap = QPixmap(C:\somedirectory\image.png")


Solution

  • Try this:

    class MyImage(QWidget):
    
        def __init__(self):
            super(MyImage, self).__init__(parent = None)
            self.mainLayout = QtGui.QHBoxLayout()
            self.setLayout(self.mainLayout)
            self.setPicture()
    
        def setPicture(self):
            picPath = os.getcwd() + "/someFolder/image.jpg"
            print(picPath) # Check if the path to your picture is correct.
            picMap = QtGui.QPixmap(picPath)
    
            picLabel = QtGui.QLabel(parent = None)
            picLabel.setGeometry(10,10,800,600)
            picLabel.setPixmap(picMap.scaled(780,580,QtCore.Qt.KeepAspectRatio))
            self.mainLayout.addWidget(picLabel)
    

    If you've got some more questions, don't hesitate to ask. I'd be happy to help you out.

    EDIT

    I've tested it all out on your code. Apparently it doesn't work for .jpg files, only for .png files. So you should convert your picture to a .png file, and then run this code (this is your code, with some minor changes):

    import os
    import sys
    
    from PyQt4.QtGui import QMainWindow
    from PyQt4.QtGui import QWidget
    from PyQt4.QtGui import QLabel
    from PyQt4.QtGui import QDockWidget
    from PyQt4.QtGui import QVBoxLayout
    from PyQt4.QtGui import QRadioButton
    from PyQt4.QtGui import QPixmap
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
    
            self.my_widget = QWidget()
            self.setCentralWidget(self.my_widget)
    
            self.createDockWindows()
            self.createButtons()
    
            self.show()
    
        def createDockWindows(self):
            #Here I create two QDockWidget and
            #I add into them a matplotlib figure created in another class
    
            # I do not have your matplotlib figures, so I've inserted
            # some labels instead.
            self.fig1 = QLabel("Hello")    #This is the matplotlib figure`s class
            self.fig2 = QLabel("World")
    
            dock1 = QDockWidget()
            dock2 = QDockWidget()
    
            dock1.setWidget(self.fig1)
            dock2.setWidget(self.fig2)
    
            self.addDockWidget(0x2, dock1)
            self.addDockWidget(0x2, dock2)
    
        def createButtons(self):
            #Here i add some buttons  
            layout = QVBoxLayout()
    
            self.r_btn = QRadioButton()
            self.r_btn.setGeometry(10, 50, 10, 30)
            self.r_btn.setText("RadioButton 1")
    
            ###HERE IS WHERE I NEED TO ADD THE IMAGE FROM THE CREATEIMAGE CLASS###
    
            self.image = MyImage()
    
            layout.addWidget(self.r_btn)
            layout.addWidget(self.image)
    
            self.my_widget.setLayout(layout)
    
    class MyImage(QWidget):
    
        def __init__(self):
            super(MyImage, self).__init__(parent = None)
            self.mainLayout = QtGui.QHBoxLayout()
            self.setLayout(self.mainLayout)
            self.setPicture()
    
        def setPicture(self):
            # For some reason, it only works on .png files, not on .jpg files!
            picPath = os.getcwd() + "\someFolder\myImage.png"
            print(picPath) # Check if the path to your picture is correct.
            picMap = QtGui.QPixmap(picPath)
    
            picLabel = QtGui.QLabel(parent = None)
            picLabel.setGeometry(10,10,800,600)
            picLabel.setPixmap(picMap.scaled(780,580,QtCore.Qt.KeepAspectRatio))
            self.mainLayout.addWidget(picLabel)
    
    
    if __name__== '__main__':
        app = QtGui.QApplication(sys.argv)
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
        myGUI = MainWindow()
    
        sys.exit(app.exec_())
    

    Please run this code, and let me know if it worked for you.

    SECOND EDIT

    Here are a few extra remarks to make it work:

    (1) The .png file should be a 'genuine' .png file. Just renaming your .jpg file into a .png file won't work. I've tried it.

    (2) Please don't forget that os.getcwd() already implies a large part of the path. Therefore it is important to check the output of the print statement: print(picPath). Check if this path is correctly referring to your picture.

    THIRD EDIT

    Even converting your .jpg picture into a .png picture using Paint will not work. If you still get the error, please go to this website to download a genuine .png format picture. And try it over with that one: http://www.flaticon.com/free-icon/mortarboard_123402