微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 PyQt5 QPixmap 和 QPushButtons 合并到 QDialog 内的 QtWidget 布局中

如何解决将 PyQt5 QPixmap 和 QPushButtons 合并到 QDialog 内的 QtWidget 布局中

一个外部模块启动了这个 QDialog 窗口,我想在其中添加标签、按钮和图像。在这个新窗口中,我正在为两个因素苦苦挣扎:1)它在按钮显示之前就点击了。 .addLayouts,图像总是出现在窗口的左上角(或在 .move 位置),而不是像预期的那样堆叠在 QHBoxLayouts 和 QVBoxLayouts 中。

外部模块调用窗口对象(见下面屏幕截图中的箭头):

def photoButtonpressed(self):
        print("Photo button pressed")          
        
        photoWidget = FieldPhotos(self.photoFP,self.photoDT,self)
        photoWidget.show()

首先,我注释掉所有与像素图/图像相关的内容以对按钮进行故障排除,并且代码会在甚至显示窗口之前自动运行 updateImagepressed 方法(传递方向变量“next”)(更不用说单击下一步按钮) :

class FieldPhotos(QtWidgets.QDialog):

    def __init__(self,photoPath,photoDT,parent=None):        
        # Paths and DTs are lists of photos/datetimes within the time limit

        super().__init__(parent)
        self.setModal(True)
        self.photoPath = photoPath
        self.photoDT = photoDT
        self.left = 300
        self.top = 300
        self.width = 800
        self.height = 1000
        self.imageSelect = 0

        self.initUI()
        
    def initUI(self):
        self.setwindowTitle(f'{self.photoDT[0]} {os.path.split(self.photoPath[self.imageSelect])[-1]}')
        self.setGeometry(self.left,self.top,self.width,self.height)
    
        # Create widgets ############################
        # self.labelImage = QtWidgets.QLabel(self)
        labelNote = QtWidgets.QLabel('Close this window to continue.',self)   
        listLabel = QtWidgets.QLabel(f'Number of images found within 90 mins of data: {len(self.photoPath)} ',self)

        self.nextButton = QtWidgets.QPushButton('>')
        self.nextButton.setToolTip('Next image in list')

        self.prevIoUsButton = QtWidgets.QPushButton('<')
        self.prevIoUsButton.setToolTip('PrevIoUs image in list')
        self.nextButton.clicked.connect(self.updateImagepressed('next'))
        self.prevIoUsButton.clicked.connect(self.updateImagepressed('prevIoUs'))
                
        # Setup Layout ##################################
        self.VBox = QtWidgets.QVBoxLayout()
        # self.VBox.addWidget(labelImage)
        self.VBox.addWidget(labelNote)

        self.HBox = QtWidgets.QHBoxLayout()
        self.HBox.addWidget(listLabel)
        self.HBox.addWidget(self.prevIoUsButton)
        self.HBox.addWidget(self.nextButton)
        self.VBox.addLayout(self.HBox)

        # # Set initial photo
        # self.pixmap = Qpixmap(self.photoPath[self.imageSelect])
        # self.pixmapScaled = self.pixmap.scaled(800,800,QtCore.Qt.KeepAspectRatio)
        # self.labelImage.setpixmap(self.pixmapScaled)
        # self.resize(self.pixmapScaled.width(),self.pixmapScaled.height())  
        # self.labelImage.move(0,100)  # Since it won't work in VBox,move down to make space?

        # self.updateImagepressed('first')

    def updateImagepressed(self,direction):
        print(f'display {direction} image: {self.photoPath[self.imageSelect]}') 

        if self.imageSelect ==0:
            self.prevIoUsButton.setdisabled(1)
        if self.imageSelect == len(self.photoPath):
            self.nextButton.setdisabled(0)
        
        if direction == 'next':
            self.imageSelect += 1
        if direction == 'prevIoUs':
            self.imageSelect -= 1

        # Set photo
        # self.pixmap = Qpixmap(self.photoPath[self.imageSelect])
        # self.pixmapScaled = self.pixmap.scaled(800,100) 

结果如下:

Photo button pressed
display next image: /Users/daurin/Projects_Supplemental/HyperPACE/field_data/HyperSAS/VIirs2019/Photos/IMG_20190908_104845.jpg
Traceback (most recent call last):
  File "/Users/daurin/GitRepos/HyperInSPACE/Source/AnomalyDetection.py",line 356,in photoButtonpressed
    photoWidget = FieldPhotos(self.photoFP,self)
  File "/Users/daurin/GitRepos/HyperInSPACE/Source/FieldPhotos.py",line 28,in __init__
    self.initUI()
  File "/Users/daurin/GitRepos/HyperInSPACE/Source/FieldPhotos.py",line 45,in initUI
    self.nextButton.clicked.connect(self.updateImagepressed('next'))
TypeError: argument 1 has unexpected type 'nonetype'"

接下来,当我注释掉按钮动作时,并像这样恢复 Qpixmap 代码

def initUI(self):
        self.setwindowTitle(f'{self.photoDT[0]} {os.path.split(self.photoPath[self.imageSelect])[-1]}')
        self.setGeometry(self.left,self.height)
    
        # Create widgets ############################
        self.labelImage = QtWidgets.QLabel(self)
        labelNote = QtWidgets.QLabel('Close this window to continue.',self)

        self.nextButton = QtWidgets.QPushButton('>')
        self.nextButton.setToolTip('Next image in list')
        
        self.prevIoUsButton = QtWidgets.QPushButton('<')
        self.prevIoUsButton.setToolTip('PrevIoUs image in list')
        # self.nextButton.clicked.connect(self.updateImagepressed('next'))
        # self.prevIoUsButton.clicked.connect(self.updateImagepressed('prevIoUs'))
                
        # Setup Layout ##################################
        self.VBox = QtWidgets.QVBoxLayout()
        self.VBox.addWidget(self.labelImage)
        self.VBox.addWidget(labelNote)

        self.HBox = QtWidgets.QHBoxLayout()
        self.HBox.addWidget(listLabel)
        self.HBox.addWidget(self.prevIoUsButton)
        self.HBox.addWidget(self.nextButton)
        self.VBox.addLayout(self.HBox)

        # Set initial photo
        self.pixmap = Qpixmap(self.photoPath[self.imageSelect])
        self.pixmapScaled = self.pixmap.scaled(800,QtCore.Qt.KeepAspectRatio)
        self.labelImage.setpixmap(self.pixmapScaled)
        self.resize(self.pixmapScaled.width(),self.pixmapScaled.height())  
        self.labelImage.move(0,move down to make space?

        # self.updateImagepressed('first')

它忽略了所有的 VBoxes 和 HBoxes,只在左上角堆积内容而忽略按钮:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。