我有一个在QDesigner中构建的UI,当我尝试连接信号和插槽时,Python会出现以下错误:
TypeError:connect()在clicked(bool)和loadClicked()
之间失败如果我删除@pyqtSlot()设计器,UI将启动,但是当我单击该按钮时,Python会出错并关闭。可能导致这个问题的原因是什么?
SSL_library_init()
我摆脱了QDesigner UI代码并试图导入UI文件。它仍然没有解决问题。修改后的代码如下。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
import cv2
import sys
class Ui_Dialog(object):
def setupUi(self, Dialog):
# Other UI Setup and config attributes
self.SelectImage.clicked.connect(self.loadClicked)
@pyqtSlot()
def loadClicked(self):
super(self.loadImage("Wilkerson.png"))
def loadImage(self, fname):
self.image = cv2.imread(fname)
self.displayImage()
def displayImage(self):
qformat=QImage.Format_Indexed8
if len(self.image.shape) == 3:
if (self.image.shape[2]) == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
img = QImage(self.image, self.image.shape[1], self.image.shape[0], self.image.strides[0], qformat)
img = img.rgbSwapped()
self.importedImageFrame.setPixmap(QPixmap.fromImage(img))
self.importedImageFrame.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
我回去重新审视了一些事情并且能够解决问题。但是,现在我遇到了一个新问题,即无法使输出图像适合我为显示图像而创建的QDsigner标签。显示预转换后的图像,但不会缩放以适合标签。我的代码挂在display_image函数的第二行到最后一行......
class ezNPR(QDialog):
def __init__(self):
super(ezNPR,self).__init__()
loadUi('ezNPRApp.ui',self)
self.Image=None
# Insert Application control commands here
self.AppStackedWidget.setCurrentIndex(0)
self.ExitPushButton.clicked.connect(QDialog.close)
self.ImagePushButton.clicked.connect(lambda:self.AppStackedWidget.setCurrentIndex(0))
self.SignaturePushButton.clicked.connect(lambda:self.AppStackedWidget.setCurrentIndex(1))
self.HelpPushButton.clicked.connect(lambda:self.AppStackedWidget.setCurrentIndex(2))
self.SelectImage.clicked.connect(self.loadClicked)
self.setWindowTitle("ezNPR Image and Photo Cropping Tool")
@pyqtSlot()
def loadClicked(self):
fname, filter = QFileDialog.getOpenFileName(self, "Open File", 'C:\\', "Image Files (*.jpg,*.png,*.bmp,*.tif)")
if fname:
self.loadImage(fname)
else:
msg = QtWidgets.QMessageBox()
msg.setText("Invalid image type selected. Please select a new image.")
msg.setInformativeText("You must selct a BMP, JPG, PNG, or TIFF")
msg.setWindowTitle("Error Loading Image")
msg.setStandardButtons(msg.Ok)
def loadImage(self, fname):
self.displayImage()
self.image = cv2.imread(fname)
def displayImage(self):
qformat = QImage.Format_Indexed8
if len(self.image.shape) == 3:
if (self.image.shape[2]) == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
img = QImage(self.image, self.image.shape[1], self.image.shape[0], self.image.strides[0], qformat)
img = img.rgbSwapped()
self.importedImageFrame.setPixmap(QPixmap.fromImage(img))
self.importedImageFrame.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)