【问题标题】:Secondary window without slot没有插槽的辅助窗口
【发布时间】:2012-12-15 11:27:59
【问题描述】:

我想为用户的设置创建一个辅助窗口,所以在我的 QMainWindow 类中,我这样调用对象设置:

self.settingsAction = QtGui.QAction('Préférences', self)
self.settingsAction.triggered.connect(lambda: Settings(self))

然后,我构建我的设置类:

class Settings():

    """ Méthode pour effectuer les réglages du programme
    par l'utilisateur """

    def __init__(self, parent):

        #TODO: faire de cette fenêtre l'enfant de la fenêtre principale
        #On crée une fenêtre secondaire

        self.parent = parent

        self.initUI()
        self.connexion()
        self.etablirSlots()


    def connexion(self):

        self.parent.options.beginGroup("Watching")
        pathes = self.parent.options.allKeys()
        if pathes:
            for each_key in pathes:
                self.list_pathes.addItem(self.parent.options.value(each_key))
        self.parent.options.endGroup()


    def etablirSlots(self):

        self.add_directory_button.clicked.connect(self.addPath)
        self.rm_directory_button.clicked.connect(self.removePath)


    def initUI(self):

        self.parent.fen_settings = QtGui.QWidget()
        self.parent.fen_settings.setWindowTitle('Préférences')

        #http://cyp1973.blogspot.fr/2009/05/qt-resource-file.html
        #On crée un QListWidget pr présenter les pathes
        self.list_pathes = QtGui.QListWidget()

        #On crée un titre pour la zone des pathes à surveiller
        label_directories = QtGui.QLabel("Dossiers surveillés :")

        #On crée les boutons d'ajout et de retrait d'un path
        self.add_directory_button = QtGui.QPushButton("Ajouter")
        self.rm_directory_button = QtGui.QPushButton("Enlever")

        #On crée une grille pr gérer la disposition des widgets
        grid_settings = QtGui.QGridLayout()
        grid_settings.addWidget(label_directories, 0, 0, 1, 1)
        grid_settings.addWidget(self.list_pathes, 1, 0, 2, 1)
        grid_settings.addWidget(self.add_directory_button, 1, 1, 1, 1)
        grid_settings.addWidget(self.rm_directory_button, 2, 1, 1, 1)

        self.parent.fen_settings.setLayout(grid_settings)
        self.parent.fen_settings.show()


    def removePath(self):

        """ Slot pour enlever un path à surveiller
        de la conf """
        print("coucou")

        #try:
        path = self.list_pathes.selectedItems()[0].text()
        print(path)

        #http://stackoverflow.com/questions/7484699/
        #pyqt4-remove-item-widget-from-qlistwidget
        #On efface le path de la liste visible
        item = self.list_pathes.takeItem(self.list_pathes.currentRow())
        del item
        #except IndexError:
            #print("Aucun path sélectionné")
            #pass


    def addPath(self):

        """ Slot pour ajouter un path à surveiller à la conf """

        #TODO: vérifier que le path à ajouter n'est pas déjà dans la liste

        #On crée une boite de dialogue pr choisir le path à ajouter
        #et on ajoute le path à la liste visible
        path = QtGui.QFileDialog.getExistingDirectory(self.parent.fen_settings, 'Open file', '/home')
        self.list_pathes.addItem(path)

但是我的辅助窗口的两个按钮似乎没有连接到我的插槽。除了这个,一切都很好,我检查过,如果我单独使用这个类,而不在参数中传递 QMainWindow,它可以完美地工作。

您知道为什么这些插槽没有连接吗?也许信号没有正确发出?

谢谢。

【问题讨论】:

    标签: python qt python-3.x pyqt signals-slots


    【解决方案1】:

    这可能是因为您没有将父级传递给按钮,这是您的代码的工作版本:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtCore, QtGui
    
    class Settings(QtGui.QDialog):
        def __init__(self, parent=None):
            super(Settings, self).__init__(parent)
    
            self.parent = parent
    
            self.setWindowTitle(u'Préférences')
    
            self.list_pathes = QtGui.QListWidget(self)
    
            self.label_directories = QtGui.QLabel(self)
            self.label_directories.setText(u"Dossiers surveillés :")
    
            self.rm_directory_button = QtGui.QPushButton(self)
            self.rm_directory_button.setText(u"Enlever")
            self.rm_directory_button.clicked.connect(self.removePath)
    
            self.add_directory_button = QtGui.QPushButton(self)
            self.add_directory_button.setText(u"Ajouter")
            self.add_directory_button.clicked.connect(self.addPath)
    
            self.gridLayout = QtGui.QGridLayout(self)
            self.gridLayout.addWidget(self.label_directories, 0, 0, 1, 1)
            self.gridLayout.addWidget(self.list_pathes, 2, 0, 2, 2)
            self.gridLayout.addWidget(self.rm_directory_button, 3, 2, 1, 1)
            self.gridLayout.addWidget(self.add_directory_button, 2, 2, 1, 1)
    
            self.show()    
        #    self.connexion()
    
        def connexion(self):
            self.parent.options.beginGroup("Watching")
            pathes = self.parent.options.allKeys()
            if pathes:
                for each_key in pathes:
                    self.list_pathes.addItem(self.parent.options.value(each_key))
    
            self.parent.options.endGroup()
    
        def removePath(self):
            try:
                path = self.list_pathes.selectedItems()[0].text()
                item = self.list_pathes.takeItem(self.list_pathes.currentRow())
                del item
    
            except IndexError:
                print(u"Aucun path sélectionné")
    
        def addPath(self):
            path = QtGui.QFileDialog.getExistingDirectory(self, 'Open file', '/home')
            self.list_pathes.addItem(path)
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            self.settingsAction = QtGui.QAction(u'Préférences', self)
            self.settingsAction.triggered.connect(lambda: Settings(self))
    
            self.toolBar = QtGui.QToolBar(self)
            self.toolBar.addAction(self.settingsAction)
    
            self.addToolBar(QtCore.Qt.ToolBarArea(QtCore.Qt.TopToolBarArea), self.toolBar)
    
    if __name__ == "__main__":
        import  sys
    
        app  = QtGui.QApplication(sys.argv)
        main = MainWindow()
        main.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 哦,我真是个笨蛋。你是对的,它工作得很好。我忘了从 QDialog 继承,并将父级交给按钮。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    相关资源
    最近更新 更多