我正在尝试将我的代码从PyQt4更新到PyQt5,但是面临信号和插槽问题。任何对我出问题的地方以及如何在PyQt5中使用此代码的帮助都将不胜感激。
以下是python抛出的错误:str(或int)对象没有属性connect。
根据PyQt5编辑代码后,出现以下错误:
文件“ C:/Users/mshrivastava/test/UI_Integrate1.py”,行184,在OkClicked中self.threadclass.progressCounter.connect(self.updateProgressBar)
AttributeError:'int'对象没有属性'connect'
请告诉我我是否缺少某些东西
这里是有效的pyqt4代码
import sys
import logging,os
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import os
import ogr
import Design
import BoundingBox
import thread
class ThreadClass(QtCore.QThread):
progressCounter=0
progCounter_MaxVal=0;
strStatus=None
#arguments
var_path=None
var_isGDB=None
var_DatasetName=None
var_DatasetPath=None
def __init__(self, parent = None):
QtCore.QThread.__init__(self, parent)
def __del__(self):
self.exiting = True
self.wait()
def run(self):
val=BoundingBox.readTXTFile(self,self.var_path,self.var_isGDB,self.var_DatasetName,self.var_DatasetPath)
if val is not None:
self.emit(QtCore.SIGNAL('Process_Done'),val)
return
#Textpath,self.checkBox_IsGDB.isChecked(),DatasetName,DatasetPath
def initializeVar(self,path,isGDB,dataname,datapath):
self.var_path=path
self.var_isGDB=isGDB
self.var_DatasetName=dataname
self.var_DatasetPath=datapath
class MyDialog(QtGui.QDialog,Design.Ui_Dialog):
def __init__(self):
super(self.__class__, self).__init__()
QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('cleanlooks'))
self.setupUi(self)
self.threadclass=None
self.lineEditInput.setEnabled(False)
self.lineEdit_Output.setEnabled(False)
self.lineEdit_FeatureClass.setEnabled(False)
self.checkBox_IsGDB.toggled.connect(self.RadioToggled)
self.btnInput.clicked.connect(self.Browseinput)
self.btnOutput.clicked.connect(self.BrowseOutput)
self.btnOK.clicked.connect(self.OkClicked)
self.btnCancel.clicked.connect(self.Cancel)
self.btn_help.clicked.connect(self.help)
def help(self):
path=str(os.path.join("help", "ITool-Chm.chm"))
thread.start_new_thread(os.system, (r"KeyHH.exe -MyHelp -#klink 'Bounding box XY to Shape' "+path,))
def Browseinput(self):
#if(not self.checkBox_IsGDB.isChecked()):
filename = str(QFileDialog.getOpenFileName(self, 'Input File', '/',"Text File (*.txt)"))
if(filename!=''):
self.lineEditInput.setText(filename)
def BrowseOutput(self):
if(not self.checkBox_IsGDB.isChecked()):
filename = str(QtGui.QFileDialog.getSaveFileName(None,'File Name & Location','/',selectedFilter='*.shp'))
else:
filename=str(QtGui.QFileDialog.getExistingDirectory(None, "Select File GDB"))
if(filename!=''):
self.lineEdit_Output.setText(filename)
def RadioToggled(self):
if(self.checkBox_IsGDB.isChecked()):
self.lineEdit_FeatureClass.setEnabled(True)
self.lblOutput.setText('Output FGDB')
else:
self.lineEdit_FeatureClass.setEnabled(False)
self.lblOutput.setText('Output ShapeFile')
def Cancel(self):
self.lblStatus.setText("Exiting Application")
if self.threadclass!=None:
self.threadclass=None
super(self.__class__, self).close()
self.emit(QtCore.SIGNAL('Progres_Counter_1'))
self.close()
def MessageBox(self,message,title):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(message)
msg.setWindowTitle(title)
msg.setStandardButtons(QMessageBox.Ok)
msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
retval = msg.exec_()
#function to be used for thread
def updateProgressBar(self,val):
self.progressBar.setValue(val)
def updateStatusBar(self,val):
self.lblStatus.setText(str(val))
def setMaxVal_prg_bar(self,val):
self.progressBar.setMaximum(val)
def threadProcessDone(self,msg):
self.MessageBox(msg,"Info")
self.resetUI()
def funLogError(self,msg):
self.MessageBox(msg,"Error")
self.resetUI()
def resetUI(self):
self.checkBox_IsGDB.setChecked(False)
self.checkBox_IsGDB.setEnabled(True)
self.btnInput.setEnabled(True)
self.btnOutput.setEnabled(True)
self.lineEdit_FeatureClass.setEnabled(False)
self.btnOK.setEnabled(True)
self.progressBar.setValue(0)
self.lblStatus.setText('Status...')
self.lineEditInput.setText('')
self.lineEdit_Output.setText('')
self.lineEdit_FeatureClass.setText('')
def UpdateUIBeforeProcess(self):
self.checkBox_IsGDB.setEnabled(False)
self.btnInput.setEnabled(False)
self.btnOutput.setEnabled(False)
self.lineEdit_FeatureClass.setEnabled(False)
self.btnOK.setEnabled(False)
def ValidateUi(self):
if(self.lineEditInput.text()==''):
self.to_be_filled_control="Input File can't be left blank"
return False
elif(self.lineEdit_Output.text()==''):
self.to_be_filled_control="Output can't be left blank"
return False
elif(self.checkBox_IsGDB.isChecked() and self.lineEdit_FeatureClass.text()==''):
self.to_be_filled_control="Feature Class Name can't be left blank"
return False
return True
def OkClicked(self):
if(self.ValidateUi()):
self.threadclass=ThreadClass()
#readTXTFile(path,isFGDB,DataSetName,DatasetPath)
Textpath=str(self.lineEditInput.text())
if(self.checkBox_IsGDB.isChecked()):
DatasetName=str(self.lineEdit_FeatureClass.text())
DatasetPath=str(self.lineEdit_Output.text())
else:
shpFullPath=str(self.lineEdit_Output.text())
DatasetPath,DatasetName=os.path.split(shpFullPath)
self.threadclass.initializeVar(Textpath,self.checkBox_IsGDB.isChecked(),DatasetName,DatasetPath)
self.threadclass.start()
# pyqt use signal and slot for communicetion between threads
self.connect(self.threadclass,QtCore.SIGNAL('Progres_Counter'),self.updateProgressBar)
self.connect(self.threadclass,QtCore.SIGNAL('Progres_Counter_Max_Val'),self.setMaxVal_prg_bar)
self.connect(self.threadclass,QtCore.SIGNAL('Status_Label'),self.updateStatusBar)
self.connect(self.threadclass,QtCore.SIGNAL('Process_Done'),self.threadProcessDone)
self.connect(self.threadclass,QtCore.SIGNAL('Log_Error'),self.funLogError)
self.UpdateUIBeforeProcess()
else:
self.MessageBox(self.to_be_filled_control,"Info")
def main():
app = QtGui.QApplication(sys.argv)
myapp = MyDialog()
myapp.show()
app.exec_()
if __name__ == '__main__':# if we're running file directly and not importing it
main() # run the main function
这是我尝试将其转换为PyQt5
import sys
import logging,os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal, QObject
from PyQt5.QtWidgets import QProgressBar
from PyQt5.QtGui import *
import os
import ogr
import Design
import BoundingBox
import _thread
class ThreadClass(QtCore.QThread):
progressCounter=0
Progress_Counter_Max_Val=100;
strStatus=None
#arguments
var_path=None
var_isGDB=None
var_DatasetName=None
var_DatasetPath=None
Process_Done = pyqtSignal(str)
def __init__(self, parent = None):
QtCore.QThread.__init__(self, parent)
def __del__(self):
self.exiting = True
self.wait()
def run(self):
val=BoundingBox.readTXTFile(self,self.var_path,self.var_isGDB,self.var_DatasetName,self.var_DatasetPath)
if val is not None:
#self.emit(QtCore.SIGNAL('Process_Done'),val)
self.Process_Done[int].emit(val)
return
#Textpath,self.checkBox_IsGDB.isChecked(),DatasetName,DatasetPath
def initializeVar(self,path,isGDB,dataname,datapath):
self.var_path=path
self.var_isGDB=isGDB
self.var_DatasetName=dataname
self.var_DatasetPath=datapath
class QDialog(QtWidgets.QDialog,Design.Ui_Dialog):
def __init__(self):
super(self.__class__, self).__init__()
QtWidgets.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create('cleanlooks'))
self.setupUi(self)
self.threadclass=None
self.lineEditInput.setEnabled(False)
self.lineEdit_Output.setEnabled(False)
self.lineEdit_FeatureClass.setEnabled(False)
self.checkBox_IsGDB.toggled.connect(self.RadioToggled)
self.btnInput.clicked.connect(self.Browseinput)
self.btnOutput.clicked.connect(self.BrowseOutput)
self.btnOK.clicked.connect(self.OkClicked)
self.btnCancel.clicked.connect(self.Cancel)
self.btn_help.clicked.connect(self.help)
def help(self):
path=str(os.path.join("help", "ITool-Chm.chm"))
_thread.start_new_thread(os.system, (r"KeyHH.exe -MyHelp -#klink 'Bounding box XY to Shape' "+path,))
def Browseinput(self):
if(not self.checkBox_IsGDB.isChecked()):
filename = str(QtWidgets.QFileDialog.getOpenFileName(self, 'Input File', '/',"Text File (*.txt)"))
if(filename!=''):
self.lineEditInput.setText(filename)
def BrowseOutput(self):
if(not self.checkBox_IsGDB.isChecked()):
filename = str(QtWidgets.QFileDialog.getSaveFileName(None,'File Name & Location','/',filter='*.shp'))
else:
filename=str(QtWidgets.QFileDialog.getExistingDirectory(None, "Select File GDB"))
if(filename!=''):
self.lineEdit_Output.setText(filename)
def RadioToggled(self):
if(self.checkBox_IsGDB.isChecked()):
self.lineEdit_FeatureClass.setEnabled(True)
self.lblOutput.setText('Output FGDB')
else:
self.lineEdit_FeatureClass.setEnabled(False)
self.lblOutput.setText('Output ShapeFile')
def Cancel(self):
self.lblStatus.setText("Exiting Application")
if self.threadclass!=None:
self.threadclass=None
super(self.__class__, self).close()
#self.emit(QtCore.SIGNAL('Progres_Counter_1'))
self.Progress_Counter_1.emit(val)
self.close()
def MessageBox(self,message,title):
msg = QtWidgets.QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(message)
msg.setWindowTitle(title)
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
retval = msg.exec_()
#function to be used for thread
def updateProgressBar(self,val):
self.progressBar.setValue(val)
def updateStatusBar(self,val):
self.lblStatus.setText(str(val))
def setMaxVal_prg_bar(self,val):
self.progressBar.setMaximum(val)
def threadProcessDone(self,msg):
self.MessageBox(msg,"Info")
self.resetUI()
def funLogError(self,msg):
self.MessageBox(msg,"Error")
self.resetUI()
def resetUI(self):
self.checkBox_IsGDB.setChecked(False)
self.checkBox_IsGDB.setEnabled(True)
self.btnInput.setEnabled(True)
self.btnOutput.setEnabled(True)
self.lineEdit_FeatureClass.setEnabled(False)
self.btnOK.setEnabled(True)
self.progressBar.setValue(0)
self.lblStatus.setText('Status...')
self.lineEditInput.setText('')
self.lineEdit_Output.setText('')
self.lineEdit_FeatureClass.setText('')
def UpdateUIBeforeProcess(self):
self.checkBox_IsGDB.setEnabled(False)
self.btnInput.setEnabled(False)
self.btnOutput.setEnabled(False)
self.lineEdit_FeatureClass.setEnabled(False)
self.btnOK.setEnabled(False)
def ValidateUi(self):
if(self.lineEditInput.text()==''):
self.to_be_filled_control="Input File can't be left blank"
return False
elif(self.lineEdit_Output.text()==''):
self.to_be_filled_control="Output can't be left blank"
return False
elif(self.checkBox_IsGDB.isChecked() and self.lineEdit_FeatureClass.text()==''):
self.to_be_filled_control="Feature Class Name can't be left blank"
return False
return True
def OkClicked(self):
if(self.ValidateUi()):
self.threadclass=ThreadClass()
Textpath=str(self.lineEditInput.text())
#readTXTFile(path,isFGDB,DataSetName,DatasetPath)
if(self.checkBox_IsGDB.isChecked()):
DatasetName=str(self.lineEdit_FeatureClass.text())
DatasetPath=str(self.lineEdit_Output.text())
else:
shpFullPath=str(self.lineEdit_Output.text())
DatasetPath,DatasetName=os.path.split(shpFullPath)
self.threadclass.initializeVar(Textpath,self.checkBox_IsGDB.isChecked(),DatasetName,DatasetPath)
self.threadclass.start()
# pyqt use signal and slot for communication between threads
self.threadclass.progressCounter.connect(self.updateProgressBar)
self.threadclass.Progress_Counter_Max_Val.connect(self.setMaxVal_prg_bar)
self.threadclass.Status_Label.connect(self.updateStatusBar)
self.threadclass.Process_Done.connect(self.threadProcessDone)
self.threadclass.Log_Error.connect(self.funLogError)
self.UpdateUIBeforeProcess()
else:
self.MessageBox(self.to_be_filled_control,"Info")
def main():
app = QtWidgets.QApplication(sys.argv)
myapp = QDialog()
myapp.show()
app.exec_()
if __name__ == '__main__':# if we're running file directly and not importing it
main() # run the main function
# I'm coming!
# here, type error!
Process_Done = pyqtSignal(str)
self.Process_Done[int].emit(val)
# solution: val type is str, and delete '[int]'