PyQt4 - 在窗口小部件上显示图像不起作用

时间:2013-02-20 09:43:52

标签: python widget pyqt4

MainWindow包含用于处理图像的菜单和函数。有一个变量self.image = QtGui.QImage()可以保存所选图片。当我在QPixmap中使用MainWindow来显示图片时 - 它有效。 但我有Splitter(QtGui.QWidget)课程QGraphicsView,我想在此展示图片。 我在Splitter类中声明了scene = QtGui.QGraphicsScene()并尝试了这个: 来自MainWindow类内部的Splitter.scene.addPixmap(QtGui.QPixmap.fromImage(image)),但它不起作用 - AttributeError: type object 'Splitter' has no attribute 'scene'. 怎么办?

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__()
        self.form_widget = Splitter()
        self.setCentralWidget(self.form_widget) 
        self.initUI()

    def initUI(self):               

        self.filename = None
        self.statusBar().showMessage('Ready')


        openFile = QtGui.QAction(QtGui.QIcon('fileopen.png'), u'Otwórz plik', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip(u'Otwórz nowy plik')
        openFile.triggered.connect(self.showDialog)

        exitAction = QtGui.QAction(QtGui.QIcon('filequit.png'), 'Zamknij', self)        

        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Zamknij program')
        exitAction.triggered.connect(QtGui.qApp.quit)


        #utworzenie menu
        menubar = self.menuBar()
        #menu Plik
        fileMenu = menubar.addMenu('&Plik')
        fileMenu.addAction(openFile)
        fileMenu.addAction(exitAction)

        #menu Edycja
        editMenu=menubar.addMenu('&Edycja')

        #menu Pomoc
        helpMenu=menubar.addMenu('&Pomoc')
        helpAction=QtGui.QAction(QtGui.QIcon('icon.png'), u'O programie',self)
        helpAction.triggered.connect(self.helpAbout)
        helpMenu.addAction(helpAction)
        helpMenu.addAction(u"Opis możliwości", self.helpAbout) #dopisać funkcję


        self.setGeometry(200, 200, 500, 500)
        self.setWindowTitle(u'System analizy rozkładu jasności obrazu')    
        self.show()


    # do otwierania pliku    
    def showDialog(self):

        dir1 = os.path.dirname(self.filename) \
                if self.filename is not None else "."
        formats = ["*.%s" % unicode(format).lower() \
                   for format in QtGui.QImageReader.supportedImageFormats()]
        fname = unicode(QtGui.QFileDialog.getOpenFileName(self,
                            u"System analizy obrazów - Wybierz obraz", dir1,
                            u"Pliki obrazów (%s)" % " ".join(formats)))
        if fname:
            self.loadFile(fname)

    def loadFile(self, fname=None):
        if fname is None:
            action = self.sender()
            if isinstance(action, QtGui.QAction):
                fname = unicode(action.data().toString())
                if not self.okToContinue():
                    return
                else:
                    return
        if fname:
            self.filename = None
            image = QtGui.QImage(fname)
        if image.isNull():
            message = u"Błąd odczytu %s" % fname
        else:
            self.image = QtGui.QImage()
            self.image = image
            self.filename = fname
            self.showImage()
            #self.dirty = False
            #self.sizeLabel.setText("%d x %d" % (image.width(), image.height()))
            message = u"Załadowano %s" % os.path.basename(fname)
        self.updateStatus(message)

        self.filename = None
        image = QtGui.QImage(fname)
        if image.isNull():
            message = u"Błąd odczytu %s" % fname


    def updateStatus(self, message):
        self.statusBar().showMessage(message, 5000)    

    def showImage(self, percent=None):
        if self.image.isNull():
            return
        #if percent is None:
        #    percent = self.zoomSpinBox.value()
        percent=50
        factor = percent / 100.0
        width = self.image.width() * factor
        height = self.image.height() * factor
        image = self.image.scaled(width, height, QtCore.Qt.KeepAspectRatio)
        Splitter.scene.addPixmap(QtGui.QPixmap.fromImage(image)) #doesn't work
        #self.imageLabelRight.setPixmap(QtGui.QPixmap.fromImage(image)) #works, but it's not on widget

class Splitter(QtGui.QWidget):

    def __init__(self):
        super(Splitter, self).__init__()

        self.initUI()

    def initUI(self):      

        #napisy wejściowe
        label_up_box = QtGui.QHBoxLayout()
        napis1=QtGui.QLabel(u"OBRAZ WEJŚCIOWY")
        napis1.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";")
        napis1.setAlignment(QtCore.Qt.AlignCenter)

        napis2=QtGui.QLabel(u"OBRAZ WYJŚCIOWY")
        napis2.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";")
        napis2.setAlignment(QtCore.Qt.AlignCenter)
        label_up_box.addWidget(napis1)
        label_up_box.addWidget(napis2)


        #dwa górne okna
        hbox = QtGui.QHBoxLayout()
        up_left = QtGui.QGraphicsView(self)
        up_right = QtGui.QGraphicsView(self)
        hbox.addWidget(up_left)
        hbox.addWidget(up_right)

        #napisy do slidera
        label_slider = QtGui.QHBoxLayout()
        napis1=QtGui.QLabel(u"\n\nREGULACJA JASNOŚCI")
        napis1.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";")
        napis1.setAlignment(QtCore.Qt.AlignCenter)
        label_slider.addWidget(napis1)


        #slider i licznik
        hbox2 = QtGui.QHBoxLayout()
        sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        lcd = QtGui.QLCDNumber(self)
        hbox2.addWidget(sld)
        hbox2.addWidget(lcd)
        sld.valueChanged.connect(lcd.display)

        #napisy do histogramów
        label_down_box = QtGui.QHBoxLayout()
        napis1=QtGui.QLabel(u"\n\nHISTOGRAM WEJŚCIOWY")
        napis1.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";")
        napis1.setAlignment(QtCore.Qt.AlignCenter)

        napis2=QtGui.QLabel(u"\n\nHISTOGRAM WYJŚCIOWY")
        napis2.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";")
        napis2.setAlignment(QtCore.Qt.AlignCenter)
        label_down_box.addWidget(napis1)
        label_down_box.addWidget(napis2)

        #dwa dolne okna
        hbox3 = QtGui.QHBoxLayout()
        down_left = QtGui.QGraphicsView(self)
        down_right = QtGui.QGraphicsView(self)
        hbox3.addWidget(down_left)
        hbox3.addWidget(down_right)

        #layout całości
        vbox = QtGui.QVBoxLayout()
        vbox.addLayout(label_up_box)
        vbox.addLayout(hbox)
        vbox.addLayout(label_slider)
        vbox.addLayout(hbox2)
        vbox.addLayout(label_down_box)
        vbox.addLayout(hbox3)


        self.setLayout(vbox)
        scene = QtGui.QGraphicsScene()  
        scene2 = QtGui.QGraphicsScene()  

        scene.addPixmap(QtGui.QPixmap('icon.png'))  
        #scene2.addPixmap(QtGui.QPixmap.fromImage(image)) #doesn't work

        up_left.setScene(scene)
        up_right.setScene(scene2)
        down_left.setScene(scene)
        down_right.setScene(scene)
        up_left.show()

        self.show()


def main():

    app = QtGui.QApplication([])
    app.setWindowIcon(QtGui.QIcon('icon.png'))

    mm = MainWindow()
    mm.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    

1 个答案:

答案 0 :(得分:0)

解决! 我将我所能做的(几乎所有内容)从MainWindow移动到Widget,并且在代码中的一些变化之后一切正常。