【问题标题】:How can i pass a modified image from a tool window to the main window?如何将修改后的图像从工具窗口传递到主窗口?
【发布时间】:2020-06-11 15:03:58
【问题描述】:

在主窗口中,我可以打开、保存或创建图像。如果我在第二个窗口中修改此图像,当我关闭第二个窗口时如何在主窗口中发送结果?

import sys
import cv2


from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.uic import loadUi

这是我传递主窗口中加载的图像并进行一些修改的第二个窗口。


class toolWindows(QtWidgets.QMainWindow):
    def __init__(self, image):
        super(toolWindows, self).__init__()
        self._image = image
        self._result = None
        menubar = self.menuBar()
        modifyImage = QtWidgets.QAction('&modifyTheImage', self)
        modifyImage.setShortcut('Ctrl+M')
        modifyImage.setStatusTip('modify the image image')
        modifyImage.triggered.connect(self.doSomething)

        fileMenu = menubar.addMenu('&modify')
        fileMenu.addAction(modifyImage)
        self.show()

    def doSomething(self):
        self._result = cv2.Canny(self._image,100,200)
        cv2.imshow('doSomething',self._result)
        return self._result

    def getResult(self):
        return self._result

这是主窗口


class mainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(mainWindow, self).__init__(*args, **kwargs)
        self.mainWindowImageSource = None
        self.mainWindowImageResult = None
        menubar = self.menuBar()
        LoadImage = QtWidgets.QAction('&LoadImage', self)
        LoadImage.setShortcut('Ctrl+L')
        LoadImage.setStatusTip('Load an image')
        LoadImage.triggered.connect(self.loadImage)

        toolWin = QtWidgets.QAction('&tool', self)
        toolWin.setShortcut('Ctrl+T')
        toolWin.setStatusTip('open the tool win')
        toolWin.triggered.connect(self.openToolWindow)

        checkResult = QtWidgets.QAction('&Check', self)
        checkResult.setShortcut('Ctrl+K')
        checkResult.setStatusTip('check that tool win pass the image on close')
        checkResult.triggered.connect(self.checkResultImage)

        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(LoadImage)
        fileMenu.addAction(toolWin)
        fileMenu.addAction(checkResult)
        self.show()

    def loadImage(self):
        fileName, filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', 'C:\\', "Image Files (*.jpg)")
        if fileName:
            self.mainWindowImageSource = cv2.imread(fileName, cv2.IMREAD_COLOR)
        else:
            print('Invalid Image')

    def openToolWindow(self):
        self.tw = toolWindows(self.mainWindowImageSource)
        self.tw.show()
        self.mainWindowImageResult = self.tw.getResult()


    def checkResultImage(self):
        print("ol")
        cv2.imshow('test', self.mainWindowImageResult)
        cv2.waitKey(0)
        cv2.destroyAllWindows()



##### APP MAIN ####
app = QtWidgets.QApplication(sys.argv)
window = mainWindow()
# this start a loop till exit
sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt5 opencv-python


    【解决方案1】:

    我通过我找到的各种帖子找到了相应的解决方案。 要从第二个窗口获得结果,最简单的方法可能是使用 QDialog。

    在主窗口中我以这种方式更改代码:

        def openToolWindow(self):
            self.tw = toolWindows(self.mainWindowImageSource)
            self.tw.show()
            check = True
            if self.tw.exec_() == QtWidgets.QDialog.Accepted:
            self.mainWindowImageResult = self.tw.getResult()
            cv2.imshow('test', self.mainWindowImageResult)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
    
    

    这样,当它打开第二个窗口(工具窗口)时,主要等待接受按钮并使用 getResult() 获取结果 - 一个简单地返回图像的函数-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多