pyqt4将图像设置为中央小部件

时间:2018-07-29 14:06:00

标签: pyqt pyqt4

我正在尝试加载图像并将该图像设置为pyqt4应用程序上的Centralwidget,但是由于某些原因,我看不到该图像。

这是我的代码-

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QScrollArea

class Canvas(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(Canvas, self).__init__()
        self.pixmap = QtGui.QPixmap()

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        openFile = QtGui.QAction("&Open File", self)
        openFile.setShortcut("Ctrl+O")
        openFile.setStatusTip('Open File')
        openFile.triggered.connect(self.file_open)
        self.scrollarea = QScrollArea()
        self.statusBar()
        mainMenu = self.menuBar()

        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)                
        self.canvas = Canvas()
        self.home()

    def home(self):
        self.show()

    def file_open(self):
        name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
        file = open(name,'r')
        with file:
            imageData = file.read()        
            image = QtGui.QImage.fromData(imageData)
            self.load_pixmap(image)

    def load_pixmap(self, image):
        self.canvas.pixmap = QtGui.QPixmap.fromImage(image)
        self.scrollarea.setWidget(self.canvas)
        self.canvas.repaint()
        self.setCentralWidget(self.scrollarea)

    def close_application(self):        
        sys.exit()                

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()     

更新的代码:

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QScrollArea

class Canvas(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(Canvas, self).__init__()
        self.pixmap = QtGui.QPixmap()

    def paintEvent(self, event):        
            return super(Canvas, self).paintEvent(event)


class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        openFile = QtGui.QAction("&Open File", self)
        openFile.setShortcut("Ctrl+O")
        openFile.setStatusTip('Open File')
        openFile.triggered.connect(self.file_open)
        self.scrollarea = QScrollArea()
        self.statusBar()
        mainMenu = self.menuBar()

        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)                
        self.canvas = Canvas()
        self.home()

    def home(self):
        self.show()

    def file_open(self):
        name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
        if name:
            self.load_pixmap(QtGui.QPixmap(name))

    def load_pixmap(self, pixmap):
        self.canvas.pixmap = pixmap
        self.scrollarea.setWidget(self.canvas)
        self.scrollarea.setWidgetResizable(True)
#         self.scrollarea.Raised
        self.canvas.repaint()
        self.setCentralWidget(self.scrollarea)

    def close_application(self):        
        sys.exit()                

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()     

1 个答案:

答案 0 :(得分:0)

画布是必须显示图像的画布,但您没有绘画它。你不觉得那是问题吗?另一方面,必须将滚动区域的属性widgetResizable设置为True。

import sys
from PyQt4 import QtGui, QtCore

class Canvas(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(Canvas, self).__init__()
        self.pixmap = QtGui.QPixmap()

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(QtCore.QPoint(), self.pixmap)

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        openFile = QtGui.QAction("&Open File", self)
        openFile.setShortcut("Ctrl+O")
        openFile.setStatusTip('Open File')
        openFile.triggered.connect(self.file_open)
        self.scrollarea = QtGui.QScrollArea()
        self.statusBar()
        mainMenu = self.menuBar()

        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)                
        self.canvas = Canvas()
        self.home()

    def home(self):
        self.show()

    def file_open(self):
        name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
        file = open(name,'r')
        with file:
            imageData = file.read()        
            image = QtGui.QImage.fromData(imageData)
            print(image.isNull())
            self.load_pixmap(image)

    def load_pixmap(self, image):
        self.canvas.pixmap = QtGui.QPixmap.fromImage(image)
        self.scrollarea.setWidget(self.canvas)
        self.scrollarea.setWidgetResizable(True)
        self.setCentralWidget(self.scrollarea)            

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()   

另一方面,我不明白您为什么使用open()加载图像,对于一个简单的任务,很多行:

import sys
from PyQt4 import QtGui, QtCore

class Canvas(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(Canvas, self).__init__()
        self.pixmap = QtGui.QPixmap()

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(QtCore.QPoint(), self.pixmap)

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        openFile = QtGui.QAction("&Open File", self)
        openFile.setShortcut("Ctrl+O")
        openFile.setStatusTip('Open File')
        openFile.triggered.connect(self.file_open)
        self.scrollarea = QtGui.QScrollArea()
        self.statusBar()
        mainMenu = self.menuBar()

        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)                
        self.canvas = Canvas()
        self.home()

    def home(self):
        self.show()

    def file_open(self):
        name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
        if name:
            self.load_pixmap(QtGui.QPixmap(name))

    def load_pixmap(self, pixmap):
        self.canvas.pixmap = pixmap
        self.scrollarea.setWidget(self.canvas)
        self.scrollarea.setWidgetResizable(True)
        self.setCentralWidget(self.scrollarea)            

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()