【问题标题】:Add multiple ImageView items to an Qt.Window to get several images within one window in python将多个 ImageView 项目添加到 Qt.Window 以在 python 的一个窗口中获取多个图像
【发布时间】:2021-08-08 19:03:33
【问题描述】:

如何在一个 QWidget 中显示多张图片?

到目前为止,我显示的一张图片如下:

import pyqtgraph as pg
from PyQt5.QtWidgets import QMainWindow, QWidget
from PyQt5.QtCore import Qt

class Plotwindow(QMainWindow):
    def __init__(self, title, *args):
        super().__init__(*args) 
        self.plotwindow=QWidget(self,Qt.Window)
        self.image=pg.ImageView(self.plotwindow)
        self.plotwindow.setWindowTitle(title)
        self.plotwindow.show()

img1 = cv2.imread("img_0_VIS.tif", cv2.IMREAD_UNCHANGED)
window1 = Plotwindow("window1")
window1.image.setImage(img1)

或者,如果这不可能,有没有办法定义窗口的位置?这样如果我使用多个窗口 - 它们就不会全部重叠?

谢谢。

【问题讨论】:

  • 您不应将小部件作为直接子级添加到 QMainWindow。相反,您应该使用QMainWindow::setCentralWidget 并使用layouts
  • 谢谢,这对我帮助很大!请参阅下面的答案;-)

标签: python qt pyqtgraph


【解决方案1】:

如果其他人对此感到疑惑,这里有一个我想出的解决方案,感谢 G.M. 的评论 - 显示 4 个 pyqtgraph ImageView 小部件 :-)

from PyQt5.QtCore import *
from PyQt5.QtGui  import *
import pyqtgraph as pg
import cv2

class MyMainWindow(QMainWindow):

    def __init__(self):
        super().__init__() # heritate QMainWindow stuff
        self.form_widget = FormWidget( "Camera Streams")  # create widget
        self.setCentralWidget(self.form_widget) # set as central widget


class FormWidget(QWidget):

    def __init__(self, title):        
        super().__init__()
        
        self.setWindowTitle(title)
        self.layout = QGridLayout(self)

        self.image=pg.ImageView(self)
        self.layout.addWidget(self.image, 0,0)
        self.image2=pg.ImageView(self)
        self.layout.addWidget(self.image2, 0,1)
        self.image3=pg.ImageView(self)
        self.layout.addWidget(self.image3, 1,0)
        self.image4=pg.ImageView(self)
        self.layout.addWidget(self.image4, 1,1)
        self.setLayout(self.layout)  


path = "your_path"
img1 = cv2.imread(path +"\\img_0_VIS.tif", cv2.IMREAD_UNCHANGED)
img2 = cv2.imread(path +"\\img_1_VIS.tif", cv2.IMREAD_UNCHANGED)
img3 = cv2.imread(path +"\\img_2_VIS.tif", cv2.IMREAD_UNCHANGED)
img4 = cv2.imread(path +"\\img_3_VIS.tif", cv2.IMREAD_UNCHANGED)

foo = MyMainWindow()
foo.form_widget.image.setImage(img1)
foo.form_widget.image2.setImage(img2)
foo.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2015-02-16
    • 2017-01-29
    • 2019-09-05
    • 2019-11-25
    相关资源
    最近更新 更多