Secondary window without slot
I would like to create a secondary windows for the user's settings, so in my class QMainWindow, I call an object Settings like this:
self.settingsAction = QtGui.QAction('Préférences', self)
self.settingsAction.triggered.connect(lambda: Settings(self))
Then, I build my Settings class:
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)
But the two buttons of my secondary window seem to not be connected to my slots. Everythin is fine except that, and I checked, if I use this class alone, without passing the QMainWindow in argument, it works perfectly.
Would you have an idea about why these slots are not connected ? Maybe the signals are not emitted properly ?
Thank you.
这可能是因为你没有把父母传递给按钮,这是你的代码的工作版本:
#!/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_())
链接地址: http://www.djcxy.com/p/39292.html
下一篇: 没有插槽的辅助窗口