Bonjour,

J'aimerai créer mes widegets personnalisés dans QtDesigner et pouvoir les réutiliser pour créer mon application principale.

Prenons un exemple simple :
Je crée un LineEdit assosié à un Label. J'appel ça un LabeledLineEdit > LabeledLineEdit.ui
Comment feriez-vous pour réutiliser ce LabeledLineEdit dans la création d'une MainWindow par exemple ?

Pour l'instant, j'ai eu l'idée de transformer le LabeledLineEdit.ui en ui_LabeledLineEdit.py grâce à pyuic :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
pyuic4 -o ui_LabeledLineEdit.py -wx LabeledLineEdit.ui
Ensuite, je créer un plugin pour QtDesigner comme dans les exemples fournis avec PyQt LabeledLineEditPlugin.py.
Enfin, je démare QtDesigner et je devrai voir apparaitre mon LabelLineEdit dans la liste des widgets mais rien n'apparait...

Pour vérrifier que ça peut marcher, j'ai copier/coller un des exemples (widget+plugin) PyQt dans les mêmes répertoires que ui_LabeledLineEdit.py et LabeledLineEditPlugin.py. Et là ça marche !

Merci

ui_configViewer.py :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from PyQt4 import QtCore, QtGui
 
class Ui_configViewer(QtGui.QWidget):
    def setupUi(self, configViewer):
        configViewer.setObjectName("configViewer")
        configViewer.resize(400, 346)
        self.cVGridLayout = QtGui.QGridLayout(configViewer)
        self.cVGridLayout.setObjectName("cVGridLayout")
        self.configViewerToolBox = QtGui.QToolBox(configViewer)
        self.configViewerToolBox.setObjectName("configViewerToolBox")
        self.configViewerToolBoxPage = Ui_configViewerToolBoxPage()
        self.configViewerToolBoxPage.setGeometry(QtCore.QRect(0, 0, 382, 264))
        self.configViewerToolBoxPage.setObjectName("configViewerToolBoxPage")
        self.configViewerToolBoPageGridLayout = QtGui.QGridLayout(self.configViewerToolBoxPage)
        self.configViewerToolBoPageGridLayout.setObjectName("configViewerToolBoPageGridLayout")
        self.configViewerToolBox.addItem(self.configViewerToolBoxPage, "")
        self.cVGridLayout.addWidget(self.configViewerToolBox, 1, 0, 1, 2)
        self.confAsCurrentPushButton = QtGui.QPushButton(configViewer)
        self.confAsCurrentPushButton.setObjectName("confAsCurrentPushButton")
        self.cVGridLayout.addWidget(self.confAsCurrentPushButton, 0, 0, 1, 1)
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.cVGridLayout.addItem(spacerItem, 0, 1, 1, 1)
 
        self.retranslateUi(configViewer)
        self.configViewerToolBox.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(configViewer)
 
    def retranslateUi(self, configViewer):
        configViewer.setWindowTitle(QtGui.QApplication.translate("configViewer", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.configViewerToolBox.setItemText(self.configViewerToolBox.indexOf(self.configViewerToolBoxPage), QtGui.QApplication.translate("configViewer", "Page 1", None, QtGui.QApplication.UnicodeUTF8))
        self.confAsCurrentPushButton.setText(QtGui.QApplication.translate("configViewer", "Exporter comme configuration courante", None, QtGui.QApplication.UnicodeUTF8))
 
from ui_configViewerToolBoxPage import Ui_configViewerToolBoxPage
 
class configViewer(QtGui.QWidget, Ui_configViewer):
    def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
        QtGui.QWidget.__init__(self, parent, f)
 
        self.setupUi(self)
 
 
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    configViewer = QtGui.QWidget()
    ui = Ui_configViewer()
    ui.setupUi(configViewer)
    configViewer.show()
    sys.exit(app.exec_())
configViewerPlugin.py :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
 
from PyQt4 import QtGui, QtDesigner
from Interface.widgets.ui_configViewer import *
 
class ConfigViewerPlugin(QtDesigner.QPyDesignerCustomWidgetPlugin, configViewer):
 
        # The __init__() method is only used to set up the plugin and define its
    # initialized variable.
    def __init__(self, parent=None):
 
        super(ConfigViewerPlugin, self).__init__(parent)
 
        self.initialized = False
 
    # The initialize() and isInitialized() methods allow the plugin to set up
    # any required resources, ensuring that this can only happen once for each
    # plugin.
    def initialize(self, core):
 
        if self.initialized:
            return
 
        self.initialized = True
 
    def isInitialized(self):
 
        return self.initialized
 
    # This factory method creates new instances of our custom widget with the
    # appropriate parent.
    def createWidget(self, parent):
        return configViewer(parent)
 
    # This method returns the name of the custom widget class that is provided
    # by this plugin.
    def name(self):
        return "ConfigViewer"
 
    # Returns the name of the group in Qt Designer's widget box that this
    # widget belongs to.
    def group(self):
        return "PyQt Examples"
 
 
    # Returns a short description of the custom widget for use in a tool tip.
    def toolTip(self):
        return ""
 
    # Returns a short description of the custom widget for use in a "What's
    # This?" help message for the widget.
    def whatsThis(self):
        return ""
 
    # Returns True if the custom widget acts as a container for other widgets;
    # otherwise returns False. Note that plugins for custom containers also
    # need to provide an implementation of the QDesignerContainerExtension
    # interface if they need to add custom editing support to Qt Designer.
    def isContainer(self):
        return False
 
    # Returns an XML description of a custom widget instance that describes
    # default values for its properties. Each custom widget created by this
    # plugin will be configured using this description.
    def domXml(self):
        return '<widget class="configViewer" name="ConfigViewer">\n' \
               ' <property name="toolTip">\n' \
               '  <string>Le template d\' une config</string>\n' \
               ' </property>\n' \
               ' <property name="whatsThis">\n' \
               '  <string>Le widget pour afficher une config</string>\n' \
               ' </property>\n' \
               '</widget>\n'
 
    # Returns the module containing the custom widget class. It may include
    # a module path.
    def includeFile(self):
        return "Interface.widgets.ui_configViewer"