【问题标题】:Conversion from PyQt4 to PyQt5从 PyQt4 到 PyQt5 的转换
【发布时间】:2020-08-03 10:11:49
【问题描述】:

我需要将 Pyqt4 应用程序升级/转换为 Pyqt5。它在使用 Pyqt4 运行时工作正常,并给出了所附屏幕截图所示的所需结果,但即使在进行了所需的更改后,它也不能在 PyQt5 上运行。在更新的代码中,菜单未连接,单击它们时没有任何反应。 如果我遗漏了什么,请帮助我。

这是 UI.py

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1192, 794)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/Main_UI/icons/icons/icon-main.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setAutoFillBackground(False)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 1201, 771))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.gridLayout_6 = QtGui.QGridLayout()
        self.gridLayout_6.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
        self.gridLayout_6.setSpacing(0)
        self.gridLayout_6.setObjectName(_fromUtf8("gridLayout_6"))
        self.horizontalLayout.addLayout(self.gridLayout_6)
        self.mdiArea = QtGui.QMdiArea(self.horizontalLayoutWidget)
        self.mdiArea.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.mdiArea.setObjectName(_fromUtf8("mdiArea"))
        self.horizontalLayout.addWidget(self.mdiArea)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1192, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuSpatial_Analyst = QtGui.QMenu(self.menubar)
        self.menuSpatial_Analyst.setObjectName(_fromUtf8("menuSpatial_Analyst"))
        self.menuTool2 = QtGui.QMenu(self.menubar)
        self.menuTool2.setObjectName(_fromUtf8("menuTool2"))
        self.menuTool3 = QtGui.QMenu(self.menubar)
        self.menuTool3.setObjectName(_fromUtf8("menuTool3"))
        self.menuHelp = QtGui.QMenu(self.menubar)
        self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
        MainWindow.setMenuBar(self.menubar)
        self.menubar.addMenu(self.menuSpatial_Analyst)
        self.menubar.addMenu(self.menuTool2)
        self.menubar.addMenu(self.menuTool3)
        self.menubar.addMenu(self.menuHelp)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Toolbox", None))
        self.menuSpatial_Analyst.setTitle(_translate("MainWindow", "Tool1", None))
        self.menuTool2.setTitle(_translate("MainWindow", "Tool2", None))
        self.menuTool3.setTitle(_translate("MainWindow", "Tool3", None))
        self.menuHelp.setTitle(_translate("MainWindow", "Help", None))

import resources_rc

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

这是 Integration.py

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import sys
import os
import thread
import frmParent_Form
import Scripts.Zonal_Stat.Qt_ZoneStat as Qt_ZoneStat
import Scripts.Tabulate_Threading.UI_Func_Integrate as UI_Func_Integrate
import Scripts.Nearby.Join_NearbyFGDB as Join_NearbyFGDB
import Scripts.MakeThematic.UI_Func_Integrate as makeThem


class MyDialog(QtGui.QMainWindow,frmParent_Form.Ui_MainWindow):
    btn_added_Tool1=False
    btn_added_Tool2=False
    btn_added_Tool3=False
    btn_added_Help=False

    def __init__(self):
        super(self.__class__, self).__init__()
        self.subwindow=None
        QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowMinimizeButtonHint)
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('cleanlooks'))
        self.setupUi(self)
        self.setFixedSize(self.size())  
        self.activeMenu=""
        self.menubar.triggered.connect(self.setUIForMenu)
        self.setStyleSheet("""
            QMenuBar {
                background-color: rgb(117, 161, 216);
                color: rgb(255,255,255);
                border: 1px solid #000;
            }

            QMenuBar::item {
                background-color: rgb(117, 161, 216);
                color: rgb(255,255,255);
            }

            QMenuBar::item::selected {
                background-color: rgb(21, 112, 216);
            }

            QMenu {
                background-color: rgb(49,49,49);
                color: rgb(255,255,255);
                border: 1px solid #000;           
            }

            QMenu::item::selected {
                background-color: rgb(21, 112, 216);
            }
        """)        

    def setUIForMenu(self):
        mdi_dimension=self.mdiArea.frameGeometry()
        self.widthDesktop  = mdi_dimension.width()         
        self.heightDesktop = mdi_dimension.height()     

        test=self.menubar.activeAction()
        strMenu= test.text()
        if strMenu=="Tool1" and self.activeMenu!="Tool1":
            self.activeMenu="Tool1"
            self.Tool1()
        elif strMenu=="Tool2" and self.activeMenu!="Tool2":
            self.activeMenu="Tool2"
            self.Tool2()
        elif strMenu=="Tool3" and self.activeMenu!="Tool3":
            self.activeMenu="Tool3"
            self.Tool3()
        elif strMenu=="Help":
            self.activeMenu="Help" 
            self.help()



    def Tool3(self):
        MyDialog.btn_added_Tool1=False
        MyDialog.btn_added_Tool2=False
        MyDialog.btn_added_Miscellaneous=False
        MyDialog.btn_added_Help=False   

        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)

        if not MyDialog.btn_added_Tool3:
            for i in reversed(range(self.gridLayout_6.count())): 
            self.gridLayout_6.itemAt(i).widget().deleteLater()

        if not MyDialog.btn_added_Tool3:

            MyDialog.btn_added_Tool3=True       


    def Tool2(self):
        MyDialog.btn_added_Tool1=False
        MyDialog.btn_added_Tool3=False
        MyDialog.btn_added_Miscellaneous=False
        MyDialog.btn_added_Help=False   

        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)

        if not MyDialog.btn_added_Tool2:
            for i in reversed(range(self.gridLayout_6.count())): 
            self.gridLayout_6.itemAt(i).widget().deleteLater()


            MyDialog.btn_added_Tool2=True   



    def Tool1(self):

        MyDialog.btn_added_Tool3=False
        MyDialog.btn_added_Miscellaneous=False
        MyDialog.btn_added_Help=False
        MyDialog.btn_added_Tool2=False

        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)    

        if not MyDialog.btn_added_Tool1:
            for i in reversed(range(self.gridLayout_6.count())): 
                self.gridLayout_6.itemAt(i).widget().deleteLater()

        if not MyDialog.btn_added_Tool1:
            widget=QWidget()
            #
            self.btn_ZonalStat = QtGui.QPushButton(self.horizontalLayoutWidget)
            self.btn_ZonalStat.setText("Zonal Stat")
            self.gridLayout_6.addWidget(self.btn_ZonalStat,0,0,1,5)
            self.btn_ZonalStat.setStyleSheet("background-color: white")
            self.btn_ZonalStat.clicked.connect(self.Add_Subwindow)
            #
            self.btn_TabulateArea =QtGui.QPushButton(self.horizontalLayoutWidget)
            self.btn_TabulateArea.setText("Tabulate Area")
            self.gridLayout_6.addWidget(self.btn_TabulateArea,1,0,1,5)
            self.btn_TabulateArea.setStyleSheet("background-color: white")
            self.btn_TabulateArea.clicked.connect(self.Add_TabulateWindow)
            #
            self.btn_NearBy = QtGui.QPushButton(self.horizontalLayoutWidget)
            self.btn_NearBy.setText("Nearby (Distance Computation)")
            self.gridLayout_6.addWidget(self.btn_NearBy,2,0,1,5)
            self.btn_NearBy.setStyleSheet("background-color: white")
            self.btn_NearBy.clicked.connect(self.Add_NearbyWindow)
            #
            self.MakeThem = QtGui.QPushButton(self.horizontalLayoutWidget)
            self.MakeThem.setText("Make Thematic")
            self.gridLayout_6.addWidget(self.MakeThem,3,0,1,5)
            self.gridLayout_6.addWidget(widget,4,0,10,10)
            self.MakeThem.setStyleSheet("background-color: white")
            self.MakeThem.clicked.connect(self.Add_MakeThemWindow)
            MyDialog.btn_added_Tool1=True   



    def help(self):

        foldPath=os.getcwd()+"\\help\\"
        path=str(os.path.join(foldPath, "Toolbox-Chm.chm"))
        thread.start_new_thread(os.system, (r"KeyHH.exe -MyHelp -#klink 'Toolbox'           "+path,))   

    def check_position(self, y, width, height, sub_window): 
        from random import randint     
        sub_window.move(int(self.widthDesktop/2)-int(width/2), int(self.heightDesktop/2)-int(height/2))     
        print("Sub Window Moved")    

    def Add_Subwindow(self):

        for i in reversed(range(self.gridLayout_6.count()-1)): 
            self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

        self.btn_ZonalStat.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);")

        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)

        widget = Qt_ZoneStat.MyDialog()
        self.subwindow = QtGui.QMdiSubWindow(self.mdiArea) 
        self.subwindow.setWidget(widget)  
        self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
        self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)
        self.mdiArea.addSubWindow(self.subwindow)

        widget_position = self.subwindow.pos()
        widget_dimensions = self.subwindow.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

        self.connect(widget,QtCore.SIGNAL('Progres_Counter_1'),self.test)
        widget.show()
        self.subwindow.show()
        self.subwindow.widget().show()    

    def Add_TabulateWindow(self):

        for i in reversed(range(self.gridLayout_6.count()-1)): 
            self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

        self.btn_TabulateArea.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);") 
        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)    
        widget = UI_Func_Integrate.MyDialog()
        self.subwindow = QtGui.QMdiSubWindow(self.mdiArea)self.subwindow.setWidget(widget)self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)  
        self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)
        self.mdiArea.addSubWindow(self.subwindow)
        widget_position = self.subwindow.pos()
        widget_dimensions = self.subwindow.frameGeometry()  
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

        self.connect(widget,QtCore.SIGNAL('Progres_Counter_1'),self.test)
        widget.show()
        self.subwindow.show()
        self.subwindow.widget().show() 

    def Add_NearbyWindow(self):

        for i in reversed(range(self.gridLayout_6.count()-1)): 
            self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

        self.btn_NearBy.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);")       

        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)    
        widget = Join_NearbyFGDB.MyDialog()
        self.subwindow = QtGui.QMdiSubWindow(self.mdiArea) 
        self.subwindow.setWidget(widget)  
        self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)  
        self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)   
        self.mdiArea.addSubWindow(self.subwindow)
        widget_position = self.subwindow.pos()
        widget_dimensions = self.subwindow.frameGeometry()
        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

        self.connect(widget,QtCore.SIGNAL('Progres_Counter_1'),self.test)
        widget.show()
        self.subwindow.show()
        self.subwindow.widget().show()  

    def Add_MakeThemWindow(self):

        for i in reversed(range(self.gridLayout_6.count()-1)): 
            self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

        self.MakeThem.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);") 

        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)    

        self.subwindow = QtGui.QMdiSubWindow(self.mdiArea) 
        widget = makeThem.MyDialog()
        self.subwindow.setWidget(widget)  
        self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
        self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30) 

        self.mdiArea.addSubWindow(self.subwindow)

        widget_position = self.subwindow.pos()
        widget_dimensions = self.subwindow.frameGeometry()

        self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

        self.subwindow.show()
        self.subwindow.widget().show()
        self.connect(widget,QtCore.SIGNAL('Progres_Counter_1'),self.test)



    def test(self):
        if self.subwindow!=None:
            self.mdiArea.removeSubWindow(self.subwindow)

if __name__ == '__main__':# if we're running file directly and not importing it
try:
   app = QtGui.QApplication(sys.argv)
   myapp = MyDialog()
   myapp.show()
   app.exec_()   
except:
   print (str(sys.exc_info()[0]))

此处的图像显示了应用程序的工作原理,每个菜单选项都有子菜单作为打开新工具窗口的按钮。

我已经更新了 Ui.py 并进行了必要的更改,即在需要的地方更改了模块。这是更新后的 PyQt5 Integration.py

    class MyDialog(QtWidgets.QMainWindow,frmParent_Form.Ui_MainWindow):
        Progres_Counter_1= pyqtSignal()
        btn_added_Tool1=False
        btn_added_Tool2=False
        btn_added_Tool3=False
        btn_added_Help=False

        def __init__(self):
            super(self.__class__, self).__init__()
            self.subwindow=None
            QtWidgets.QDialog.__init__(self, None, QtCore.Qt.WindowMinimizeButtonHint)
            QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create('cleanlooks'))
            self.setupUi(self)
            self.setFixedSize(self.size())  
            self.activeMenu=""
            self.menubar.triggered.connect(self.setUIForMenu)
            self.setStyleSheet("""
                QMenuBar {
                    background-color: rgb(117, 161, 216);
                    color: rgb(255,255,255);
                    border: 1px solid #000;
                }

                QMenuBar::item {
                    background-color: rgb(117, 161, 216);
                    color: rgb(255,255,255);
                }

                QMenuBar::item::selected {
                    background-color: rgb(21, 112, 216);
                }

                QMenu {
                    background-color: rgb(49,49,49);
                    color: rgb(255,255,255);
                    border: 1px solid #000;           
                }

                QMenu::item::selected {
                    background-color: rgb(21, 112, 216);
                }
            """)        

        def setUIForMenu(self):
            mdi_dimension=self.mdiArea.frameGeometry()
            self.widthDesktop  = mdi_dimension.width()         
            self.heightDesktop = mdi_dimension.height()     

            test=self.menubar.activeAction()
            strMenu= test.text()
            if strMenu=="Tool1" and self.activeMenu!="Tool1":
                self.activeMenu="Tool1"
                self.Tool1()
            elif strMenu=="Tool2" and self.activeMenu!="Tool2":
                self.activeMenu="Tool2"
                self.Tool2()
            elif strMenu=="Tool3" and self.activeMenu!="Tool3":
                self.activeMenu="Tool3"
                self.Tool3()
            elif strMenu=="Help":
                self.activeMenu="Help" 
                self.help()



        def Tool3(self):
            MyDialog.btn_added_Tool1=False
            MyDialog.btn_added_Tool2=False
            MyDialog.btn_added_Miscellaneous=False
            MyDialog.btn_added_Help=False   

            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)

            if not MyDialog.btn_added_Tool3:
                for i in reversed(range(self.gridLayout_6.count())): 
                self.gridLayout_6.itemAt(i).widget().deleteLater()

            if not MyDialog.btn_added_Tool3:

                MyDialog.btn_added_Tool3=True       


        def Tool2(self):
            MyDialog.btn_added_Tool1=False
            MyDialog.btn_added_Tool3=False
            MyDialog.btn_added_Miscellaneous=False
            MyDialog.btn_added_Help=False   

            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)

            if not MyDialog.btn_added_Tool2:
                for i in reversed(range(self.gridLayout_6.count())): 
                self.gridLayout_6.itemAt(i).widget().deleteLater()


                MyDialog.btn_added_Tool2=True   



        def Tool1(self):

            MyDialog.btn_added_Tool3=False
            MyDialog.btn_added_Miscellaneous=False
            MyDialog.btn_added_Help=False
            MyDialog.btn_added_Tool2=False

            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)    

            if not MyDialog.btn_added_Tool1:
                for i in reversed(range(self.gridLayout_6.count())): 
                self.gridLayout_6.itemAt(i).widget().deleteLater()

            if not MyDialog.btn_added_Tool1:
                #
                widget=QWidget()
                self.btn_ZonalStat = QtWidgets.QPushButton(self.horizontalLayoutWidget)
                self.btn_ZonalStat.setText("Zonal Stat")
                self.gridLayout_6.addWidget(self.btn_ZonalStat,0,0,1,5)
                self.btn_ZonalStat.setStyleSheet("background-color: white")
                self.btn_ZonalStat.clicked.connect(self.Add_Subwindow)
                #
                self.btn_TabulateArea = QtWidgets.QPushButton(self.horizontalLayoutWidget)
                self.btn_TabulateArea.setText("Tabulate Area")
                self.gridLayout_6.addWidget(self.btn_TabulateArea,1,0,1,5)
                self.btn_TabulateArea.setStyleSheet("background-color: white")
                self.btn_TabulateArea.clicked.connect(self.Add_TabulateWindow)
                #
                self.btn_NearBy = QtWidgets.QPushButton(self.horizontalLayoutWidget)
                self.btn_NearBy.setText("Nearby (Distance Computation)")
                self.gridLayout_6.addWidget(self.btn_NearBy,2,0,1,5)
                self.btn_NearBy.setStyleSheet("background-color: white")
                self.btn_NearBy.clicked.connect(self.Add_NearbyWindow)
                #
                self.MakeThem = QtWidgets.QPushButton(self.horizontalLayoutWidget)
                self.MakeThem.setText("Make Thematic")
                self.gridLayout_6.addWidget(self.MakeThem,3,0,1,5)
                self.gridLayout_6.addWidget(widget,4,0,10,10)
                self.MakeThem.setStyleSheet("background-color: white")
                self.MakeThem.clicked.connect(self.Add_MakeThemWindow)
                MyDialog.btn_added_Tool1=True   



        def help(self):
            #os.system(r"KeyHH.exe -MyHelp -#klink 'Zonal Stats' C:\Users\nkandpal\Desktop\package\ITool.chm::Zonal Stats")    
            foldPath=os.getcwd()+"\\help\\"
            path=str(os.path.join(foldPath, "Toolbox-Chm.chm"))
            _thread.start_new_thread(os.system, (r"KeyHH.exe -MyHelp -#klink 'Toolbox' "+path,))   

        def check_position(self, y, width, height, sub_window): 
            from random import randint     
            sub_window.move(int(self.widthDesktop/2)-int(width/2), int(self.heightDesktop/2)-int(height/2))     
            print("Sub Window Moved")    

        def Add_Subwindow(self):

            for i in reversed(range(self.gridLayout_6.count()-1)): 
                self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

            self.btn_ZonalStat.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);")

            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)

            widget = Qt_ZoneStat.MyDialog()
            self.subwindow = QtWidgets.QMdiSubWindow(self.mdiArea) 
            self.subwindow.setWidget(widget)  
            self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
            self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)   
            #self.subwindow.setWindowTitle("GeoHazzard Aggregation")
            self.mdiArea.addSubWindow(self.subwindow)

            widget_position = self.subwindow.pos()
            widget_dimensions = self.subwindow.frameGeometry()
            self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

            self.widget.Progres_Counter_1.connect(self.test)
            widget.show()
            self.subwindow.show()
            self.subwindow.widget().show()    

        def Add_TabulateWindow(self):

            for i in reversed(range(self.gridLayout_6.count()-1)): 
                self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

            self.btn_TabulateArea.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);") 
            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)    
            widget = UI_Func_Integrate.MyDialog()
            self.subwindow = QtWidgets.QMdiSubWindow(self.mdiArea) 
            self.subwindow.setWidget(widget)  
            self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
            self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)   
            #self.subwindow.setWindowTitle("GeoHazzard Aggregation")
            self.mdiArea.addSubWindow(self.subwindow)

            widget_position = self.subwindow.pos()
            widget_dimensions = self.subwindow.frameGeometry()
            self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

            self.widget.Progres_Counter_1.connect(self.test)
            widget.show()
            self.subwindow.show()
            self.subwindow.widget().show() 

        def Add_NearbyWindow(self):

            for i in reversed(range(self.gridLayout_6.count()-1)): 
                self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

            self.btn_NearBy.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);")       

            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)    
            widget = Join_NearbyFGDB.MyDialog()
            self.subwindow = QtWidgets.QMdiSubWindow(self.mdiArea) 
            self.subwindow.setWidget(widget)  
            self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
            #val= widget.size().height()
            self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)   
            self.mdiArea.addSubWindow(self.subwindow)

            widget_position = self.subwindow.pos()
            widget_dimensions = self.subwindow.frameGeometry()
            self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

            self.widget.Progres_Counter_1.connect(self.test)
            widget.show()
            self.subwindow.show()
            self.subwindow.widget().show()  

        def Add_MakeThemWindow(self):

            for i in reversed(range(self.gridLayout_6.count()-1)): 
                self.gridLayout_6.itemAt(i).widget().setStyleSheet("background-color: white")   

            self.MakeThem.setStyleSheet("background-color: rgb(21, 112, 216);color: rgb(255,255,255);") 

            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)    

            self.subwindow = QtWidgets.QMdiSubWindow(self.mdiArea) 
            widget = makeThem.MyDialog()
            self.subwindow.setWidget(widget)  
            self.subwindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.WindowMinimizeButtonHint)
            self.subwindow.setFixedSize(widget.size().width()+10,widget.size().height()+30)   
            #self.subwindow.setWindowTitle("GeoHazzard Aggregation")

            self.mdiArea.addSubWindow(self.subwindow)

            widget_position = self.subwindow.pos()
            widget_dimensions = self.subwindow.frameGeometry()
            self.check_position(y=widget_position.y(), width=widget_dimensions.width(), height=widget_dimensions.height(), sub_window=self.subwindow)   

            self.subwindow.show()
            self.subwindow.widget().show()
            self.widget.Progres_Counter_1.connect(self.test)



        def test(self):
            if self.subwindow!=None:
                self.mdiArea.removeSubWindow(self.subwindow)

    if __name__ == '__main__':# if we're running file directly and not importing it
        try:
            app = QtWidgets.QApplication(sys.argv)
            myapp = MyDialog()
            myapp.show()
            app.exec_()   
        except:
            print (str(sys.exc_info()[0]))

【问题讨论】:

  • 错误是什么?
  • 你真的不应该编辑使用 pyuic 生成的文件,从不。这些文件应仅用作导入模块,如有关using Designer 的指南中所述。要将 ui 文件移植到 Qt5,您必须运行 pyuic5 来生成新的更新文件。不要手动操作。
  • @eyllanesc 菜单没有连接,点击它们没有任何反应
  • 1) 将QtWidgets.QDialog.__init__(self, None, QtCore.Qt.WindowMinimizeButtonHint) 更改为QtWidgets.QMainWindow.__init__(self, None, QtCore.Qt.WindowMinimizeButtonHint)
  • @eyllanesc 它仍然没有任何区别

标签: python pyqt pyqt5 pyqt4


【解决方案1】:

这些操作不起作用的原因是您使用了非常错误的方法来“获取”它们。

我不知道setUIForMenu() 在您使用 PyQt4 时是否有效(我认为它不应该有效,但它的行为可能因平台而异),但您当然不应该使用 @987654321 @:

返回当前突出显示的动作,如果当前没有突出显示动作,则返回 nullptr。

显然,这不是您的目的所需要的,但最重要的是,这一切都是完全不必要的:QMenuBar.triggered() 已经返回已触发的操作。

    def setUIForMenu(self, action):
        strMenu = action.menu().title()

除此之外,你的问题还有很多问题。

  • 首先,正如 cmets 中已经指出的那样,使用pyuic 生成的文件应该绝不被修改。阅读有关 using Designer 的更多信息,了解使用这些文件的正确方法。
  • 由于上述原因,如果你想迁移到 Qt5,你显然不应该手动编辑这些文件,而只需使用正确版本的 pyuic(通常命名为pyuic5)来构建 Qt5 兼容文件。然后不理会这些文件,永远不要编辑它们,只在需要更改它们的源 ui 文件时用 pyuic 覆盖它们。
  • 设置类属性来存储应该是实例的变量是很糟糕的:所有这些MyDialog.btn_added_Tool1 = ... 应该是self.btn_added_Tool1 等等,因为您将它们用作实例属性。
  • 本地导入只应在绝对必要且绝对谨慎的情况下进行:将 from random import randint 与其他导入一起放在脚本的开头。
  • 总是喜欢“新”连接样式(object.signal.connect(slot)),因为使用QtCore.SIGNAL 的“旧”方法几乎总是不必要和不鼓励的(另外,AFAIK,在最新的 PyQt5 版本中,QtCore.SIGNAL 甚至不可用不再)。
  • 不应在 QWidget 的 __init__ 中设置应用程序范围的样式。
  • test 函数中,您将删除子窗口,但您没有将subwindow 变量设置回None
  • 使用字符串进行对象比较不是一个好主意:您可能决定更改菜单/项目文本,字符串可能已本地化,或者样式可能会添加超出您控制范围的“&”助记符;请改用 对象实例(例如:if action.menu() == self.menuTool2: ...)。
  • 最后,与名称样式更加一致,并可能遵循Python style guide for code(又名,PEP-8);最重要的是,在您的情况下,函数名称不应大写;此外,当等号用于变​​量赋值时,通常最好在它们周围放置空格,因为这样可以提高可读性。

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2018-02-24
    • 2017-04-14
    • 2018-05-19
    • 1970-01-01
    • 2017-11-09
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多