繁体   English   中英

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

[英]PyQt4 - Displaying images on widget doesn't work

MainWindow类包含用于处理图像的菜单和功能。 有一个变量self.image = QtGui.QImage()保存选择的图片。 当我在MainWindow使用QPixmap显示图片时,它可以工作。 但是我有带有QGraphicsView Splitter(QtGui.QWidget)类,我想在此上显示图片。 我在Splitter类中声明了scene = QtGui.QGraphicsScene()并尝试了此操作:从MainWindow类内部进行了Splitter.scene.addPixmap(QtGui.QPixmap.fromImage(image)) ,但它不起作用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()    

解决了! 我将我可以(几乎所有)的东西从MainWindow移到了Widget并且在代码中进行了少量更改之后,一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM