IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PyQt Python Discussion :

Pointeur sur un objet


Sujet :

PyQt Python

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut Pointeur sur un objet
    Salut a tous, je voudrai amélioré mon programme donc je poste a nouveau chez vous.

    Dans ma ComboBox, j'ai des nom d'objet (ex : toto, tutu, tata)

    Ainsi comment faire pour attribuer une variable qui pointe sur le nom de l'objet dans le ComboBox?

    Sachant que dans la variable, j'ai mis des valeur de Spinbox grâce à : item = [self.SpinBox_1.setValue(1)]; item = [self.SpinBox_2.setValue(2)]

    Item est ma variable.

    Merci d'avance.

  2. #2
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Salut,

    Je pense que tu dois te tromper quelque part parce que QSpinbox.setValue() retourne None

    Donc avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    item = [self.SpinBox_1.setValue(1)]
    item = [None]

    Il n'y a pas de pointeur individuel possible sur les items d'une combo box

    Tu peux accéder au texte d'un item avec son index
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    text = mycombo.itemText(index)
    Si tu cherches plutôt où se trouve un item dans la combo:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    index = mycombo.findText('texte')
    et encore d'autres possibilités: http://pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Salut merci pour la réponse

    Si je prend : text = mycombo.itemText(index), celui-ci prend en compte l'élément choisi dans le combobox (corrige moi si c'est faux).

    Ainsi il faut attribuer une variable, dans lequel on a une liste des valeurs de Spinbox, qui pointe sur l'index sélectionné.

    Donc comme faire cela, je suis perdu dans la doc QComboBox.

    Voici l'idée :

    text = mycombo.itemText(index)
    toto = spinbox.setValue(1)

    Et comment affecter cette variable à l'élément du combobox choisie ?

    Merci d'avance

  4. #4
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Pas sur de comprendre.

    Tu veux synchroniser les deux widgets ?

    Donc si on change la valeur de la spinbox on change l'item courant de la combo et si on change l'item de la combo on met son index dans la spinbox ?

    Très simple à faire avec des signaux, mais est-ce bien cela ?

  5. #5
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    C'est pas du tout une synchronisation.

    Je crée une variable dans lequel je met des valeurs dans les Spinbox, et je ne touche plus ce variable.

    Ensuite j'affecte cette variable à un objet du Combobox,

    Et si tout va bien, après avoir cliqué sur l'objet, la spinbox reçoit la valeur que j'ai définie dans la variable.

    Merci d'avance

  6. #6
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Citation Envoyé par dhoomh Voir le message
    Je crée une variable dans lequel je met des valeurs dans les Spinbox, et je ne touche plus ce variable.
    Un exemple sera nécessaire, je crains.

    Ensuite j'affecte cette variable à un objet du Combobox,
    Pas possible, tel que dit ainsi en tous cas.


    Au fait, il n'y a pas de variable en Python.

  7. #7
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Je crée ma ComboBox :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    self.ComboBox_0 = QtGui.QComboBox()
    self.ComboBox_0.setGeometry(QtCore.QRect(10,10,10,10))
    self.ComboBox_0.addItem("HANDSET")
    self.ComboBox_0.addItem("HANDFREE")
    self.ComboBox_0.addItem("CAR KIT")
    self.ComboBox_0.addItem("RSM")
    je crée un Spinbox :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    self.SpinBox_1 = QtGui.QSpinBox(self)
    self.SpinBox_1.setRange(-12,12)
    self.SpinBox_1.setSingleStep(1)
    self.SpinBox_1.setValue(0)
    Je voudrai écrire la valeur "5" dans la SpinBox_1, en cliquant sur "HANDSET"

    Merci d'avance

  8. #8
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Ok, il n'y a aucune correspondance entre l'item de la combo et la valeur affichée dans la spinbox.

    Dans ce cas:

    Soit tu gardes tes données dans un dictionnaire, par exemple: {"HANDSET": 5, "HANDFREE": 10, "CAR KIT": 15, "RSM": 20}

    Soit tu uses des itemData qu'il est possible d'associer aux items des combos.


    Première variante:
    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
     
    # -*- coding: utf-8 -*-
    import sys
    from PyQt4.QtCore import Qt
    from PyQt4.QtGui import (QApplication, QWidget, QVBoxLayout, QSpinBox, QComboBox)
     
    class Widget(QWidget):
        def __init__(self):
            super(Widget, self).__init__()
            self.layout = QVBoxLayout(self)
            self.spin = QSpinBox(self)
            self.combo = QComboBox(self)
            self.layout.addWidget(self.spin)
            self.layout.addWidget(self.combo)
            self.combo.currentIndexChanged['QString'].connect(self.on_combo_changed)
            self.data = {"HANDSET": 5, "HANDFREE": 10, "CAR KIT": 15, "RSM": 20}
            self.combo.addItems(self.data.keys())
     
        def on_combo_changed(self, txt):
            self.spin.setValue(self.data[unicode(txt)])
     
    if __name__ == '__main__':
        app = QApplication([])
        w = Widget()
        w.show()
        sys.exit(app.exec_())
    Variante itemData:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    ...
            self.combo.currentIndexChanged.connect(self.on_combo_changed)
            data = {"HANDSET": 5, "HANDFREE": 10, "CAR KIT": 15, "RSM": 20}
            for idx, key in enumerate(data.keys()):
                self.combo.addItem(key)
                self.combo.setItemData(idx, data[key], Qt.UserRole)
     
        def on_combo_changed(self, idx):
            self.spin.setValue(self.combo.itemData(idx).toInt()[0])

  9. #9
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Merci, cela fonctionne.

    Mais voici mon programme :
    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    #!/usr/bin/env python
    # -*- coding: cp1252 -*-
    #SPEC 0.1
     
    import sys
    import time
    import serial
    import binascii
     
    from PyQt4 import QtCore, QtGui
     
    class MyPopup(QtGui.QWidget):
        def __init__(self):
            QtGui.QWidget.__init__(self)
     
            ''' Titre_1  '''
            self.Label_1 = QtGui.QLabel()
            self.Label_1.setText("NUMERO DE PORT")
     
            ''' Titre_11  '''
            self.Label_11 = QtGui.QLabel()
            self.Label_11.setText("VITESSE")
     
            ''' ComboBox_11 '''
            self.ComboBox_11 = QtGui.QComboBox()
            self.ComboBox_11.setGeometry(QtCore.QRect(10,10,10,10))
            self.ComboBox_11.addItem("COM 1")
            self.ComboBox_11.addItem("COM 25")
            self.ComboBox_11.addItem("COM 2")
            self.ComboBox_11.addItem("COM 4")
     
            ''' ComboBox_22 '''
            self.ComboBox_22 = QtGui.QComboBox()
            self.ComboBox_22.setGeometry(QtCore.QRect(10,10,10,10))
            self.ComboBox_22.addItem("4800")
            self.ComboBox_22.addItem("115200")
     
            self.bouton1 = QtGui.QPushButton("Open", self)
            self.bouton1.clicked.connect(self.F_Open)
     
            self.bouton2 = QtGui.QPushButton("Close", self)
            self.bouton2.clicked.connect(self.F_Close)
     
            # positionner les widgets dans la fenêtre
            posit = QtGui.QGridLayout()
            posit.addWidget(self.Label_1,            0, 0)
            posit.addWidget(self.ComboBox_11,        1, 0)
            posit.addWidget(self.Label_11,           2, 0)
            posit.addWidget(self.ComboBox_22,        3, 0)
            posit.addWidget(self.bouton1,            4, 0)
            posit.addWidget(self.bouton2,            5, 0)
            self.setLayout(posit)
     
     
            # SERIAL #
     
        def F_Open(self):
            F_Serial(1,self.ComboBox_11.itemText(self.ComboBox_11.currentIndex()),self.ComboBox_22.itemText(self.ComboBox_22.currentIndex()))
     
     
        def F_Close(self):
            F_Serial(4,"null", "null")
     
     
        def M_Signal(self):
            self.M_Status(3)
            time.sleep(0.2)
            self.M_Status(2)
     
     
    def F_Serial(a,port_ou_msg,vitesse):
        s = "null"   
        if a == 1:
             num_port = int(port_ou_msg[4:6])
             vit = int(vitesse)
             global ser
             ser = serial.Serial(num_port-1,vit,timeout=0,parity='N', rtscts=1)
             print ser.portstr # check which port was really used
        elif a == 2:
             ser.write(port_ou_msg+"\r\n") # Ecriture verticale
        elif a == 3:
             s = ser.readline(120)
        elif a == 4:
             ser.close()
        return s
     
     
     
     
    class essai(QtGui.QMainWindow):
        def __init__(self):
    #--------------------------------------------
            # Main Window #
            QtGui.QMainWindow.__init__(self)
            self.resize(310,310)
            self.setWindowTitle(' Travaille ')
     
            # Menu #
            ''' Menu0 '''
            Action1 = QtGui.QAction('LOAD SGV', self)   
            Action1.setStatusTip('Action 1')  
            Action1.triggered.connect(self.M_Action1)
     
            Action2 = QtGui.QAction('SAVE SGV', self)   
            Action2.setStatusTip('Action 2')  
            Action2.triggered.connect(self.M_Action2)
     
            Action3 = QtGui.QAction('EXPORT TO MOBILE', self)   
            Action3.setStatusTip('Action 3')
            Action3.triggered.connect(self.M_Action3)
     
            menubar = self.menuBar()
     
            ChMenu = menubar.addMenu('&Action')
            ChMenu.addAction(Action1)
            ChMenu.addAction(Action2)
            ChMenu.addAction(Action3)
     
            '''Menu2'''
            PopFen1 = QtGui.QAction('PopUp', self)
            PopFen1.setStatusTip('PopFen1')  
            PopFen1.triggered.connect(self.F_Popup)
     
            menubar = self.menuBar()
     
            PopMenu = menubar.addMenu('&PopFen')
            PopMenu.addAction(PopFen1)
     
    #--------------------------------------------
            ''' Dock '''
            self.ComboBox_0 = QtGui.QComboBox()
            self.ComboBox_0.setGeometry(QtCore.QRect(10,10,10,10))
            self.ComboBox_0.addItem("HANDSET")
            self.ComboBox_0.addItem("HANDFREE")
            self.ComboBox_0.addItem("CAR KIT")
            self.ComboBox_0.addItem("RSM")
     
            self.ComboBox_1 = QtGui.QComboBox()
            self.ComboBox_1.setGeometry(QtCore.QRect(10,10,10,10))
            self.ComboBox_1.addItem("VOICE")
            self.ComboBox_1.addItem("RING")
            self.ComboBox_1.addItem("BEEP")
     
            VBoxLayout_1 = QtGui.QVBoxLayout()
            VBoxLayout_1.addWidget(self.ComboBox_0)
            VBoxLayout_1.addWidget(self.ComboBox_1)
            VBoxLayout_1.setSpacing(200)
     
            Widget_1 = QtGui.QWidget()
            Widget_1.setLayout(VBoxLayout_1)
     
            F_Dock_Wid_Vitesse = QtGui.QDockWidget()
            F_Dock_Wid_Vitesse.setWidget(Widget_1)
            self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, F_Dock_Wid_Vitesse)
     
            self.SpinBox_1 = QtGui.QSpinBox(self)
            self.SpinBox_1.setRange(-12,12)
            self.SpinBox_1.setSingleStep(1)
            self.SpinBox_1.setValue(0)
     
            self.SpinBox_2 = QtGui.QSpinBox(self)
            self.SpinBox_2.setRange(-12,12)
            self.SpinBox_2.setSingleStep(1)
            self.SpinBox_2.setValue(0)
     
            self.SpinBox_3 = QtGui.QSpinBox(self)
            self.SpinBox_3.setRange(-12,12)
            self.SpinBox_3.setSingleStep(1)
            self.SpinBox_3.setValue(0)
     
            self.SpinBox_4 = QtGui.QSpinBox(self)
            self.SpinBox_4.setRange(-12,12)
            self.SpinBox_4.setSingleStep(1)
            self.SpinBox_4.setValue(0)
     
            self.SpinBox_5 = QtGui.QSpinBox(self)
            self.SpinBox_5.setRange(-12,12)
            self.SpinBox_5.setSingleStep(1)
            self.SpinBox_5.setValue(0)
     
            self.SpinBox_6 = QtGui.QSpinBox(self)
            self.SpinBox_6.setRange(-12,12)
            self.SpinBox_6.setSingleStep(1)
            self.SpinBox_6.setValue(0)
     
            GridLayout_1 = QtGui.QGridLayout()
            GridLayout_1.addWidget(self.SpinBox_1,     0,0,1,1)
            GridLayout_1.addWidget(self.SpinBox_2,     0,1,1,1)
            GridLayout_1.addWidget(self.SpinBox_3,     0,2,1,1)
            GridLayout_1.addWidget(self.SpinBox_4,     1,0,1,1)
            GridLayout_1.addWidget(self.SpinBox_5,     1,1,1,1)
            GridLayout_1.addWidget(self.SpinBox_6,     1,2,1,1)
     
            O_GroupBox_1 = QtGui.QGroupBox()
            O_GroupBox_1.setLayout(GridLayout_1)
     
            self.setCentralWidget(O_GroupBox_1)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.changed)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.changed)
     
            self.data = {"HANDFREE" : 5}
            self.data = {"RING": 6}
            self.ComboBox_0.addItems(self.data.keys())
            self.ComboBox_1.addItems(self.data.keys())
     
        def changed(self, txt):
            self.SpinBox_1.setValue(self.data[unicode(txt)])
            self.SpinBox_2.setValue(self.data[unicode(txt)])
     
     
     
     
     
     
    #--------------------------------------------
        def M_Action1(self):
     
            with open("C:/Users/ssrinivasa/Desktop/texto.txt") as f:
                x = int(f.readline().rstrip())                     
                self.SpinBox_1.setValue(x)       
                x = int(f.readline().rstrip())
                self.SpinBox_2.setValue(x)
                x = int(f.readline().rstrip())
                self.SpinBox_3.setValue(x)
                x = int(f.readline().rstrip())
                self.SpinBox_4.setValue(x)
                x = int(f.readline().rstrip())
                self.SpinBox_5.setValue(x)
                x = int(f.readline().rstrip())
                self.SpinBox_6.setValue(x)
    #--------------------------------------------
        def M_Action2(self):
     
            reponse = self.SpinBox_1.value()
            fichier = open("C:/Users/ssrinivasa/Desktop/fichier.txt", "a")
            fichier.write(str(reponse) +"\r\n") # ajouter nouvelle ligne après écriture
            reponse = self.SpinBox_2.value()
            fichier.write(str(reponse) +"\r\n")
            reponse = self.SpinBox_3.value()        
            fichier.write(str(reponse) +"\r\n")        
            reponse = self.SpinBox_4.value()        
            fichier.write(str(reponse) +"\r\n")
            reponse = self.SpinBox_5.value()        
            fichier.write(str(reponse) +"\r\n")
            reponse = self.SpinBox_6.value()     
            fichier.write(str(reponse) +"\r\n")
     
            print reponse
    #-------------------------------------------
        def M_Action3(self):
            self.Serial_Write()
     
     
        def Serial_Write(self):
            value = str(self.SpinBox_1.value())
            F_Serial(2,value,"null")
            value = str(self.SpinBox_2.value())
            F_Serial(2,value,"null")
            value = str(self.SpinBox_3.value())
            F_Serial(2,value,"null")
            value = str(self.SpinBox_4.value())
            F_Serial(2,value,"null")
            value = str(self.SpinBox_5.value())
            F_Serial(2,value,"null")
            value = str(self.SpinBox_6.value())
            F_Serial(2,value,"null")
    #--------------------------------------------
        def F_Popup(self):
            self.w = MyPopup()
            self.w.setGeometry(QtCore.QRect(100, 100, 400, 200))
            self.w.show()
    #--------------------------------------------
     
    def main():
            '''Application'''
            App = QtGui.QApplication(sys.argv)
            App.setStyle("windows")
            MainWin2 = essai()
            MainWin2.show()
            editor = essai()
            sys.exit(App.exec_() )
     
    if __name__=='__main__':
        main()
    Entre la ligne 199 et 209.

    Je voudrai prendre en compte les deux objets de la Combobox, et entrer des définir des valeurs dans les Spinbox comme tu a fait, mais la il faut prendre compte des 2 combobox.

    A tu une idée ?

    Merci d'avance

  10. #10
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Ton code est aussi confus que tes explications.

    Comment comprendre ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
            # ligne 112
            menubar = self.menuBar()
            ...
            # 12 lignes plus loin
            ...
            menubar = self.menuBar()
    Même chose ici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
            self.data = {"HANDFREE" : 5}
            self.data = {"RING": 6}
    Ce ne serait pas plutôt self.data = {"HANDFREE" : 5, "RING": 6} ?

    Et ici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
        def changed(self, txt):
            self.SpinBox_1.setValue(self.data[unicode(txt)])
            self.SpinBox_2.setValue(self.data[unicode(txt)])
    Les deux spinBoxes affichent toujours la même chose, c'est utile ?

  11. #11
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Salut merci pour ta réponse,

    J'ai effectivement adapter ta réponse à mon programme, mais je n'arrive pas à afficher les valeurs dans les Spinbox, lors du choix des deux ComboBox.

    Je remarque qu'il fait part ordre chronologique, et prend en compte juste le premier paramètre.

    Voici le code :

    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
     
    self.ComboBox_0.currentIndexChanged['QString'].connect(self.changed)
    self.ComboBox_1.currentIndexChanged['QString'].connect(self.changed)
    self.data = {"HANDFREE","RING"}
     
    def changed(self, txt):
         self.SpinBox_1.setValue(4)
         self.SpinBox_2.setValue(8)
         self.SpinBox_3.setValue(1)
         self.SpinBox_4.setValue(2)
         self.SpinBox_5.setValue(3)
         self.SpinBox_6.setValue(7)
     
    #--------------------------------------------
    self.ComboBox_0.currentIndexChanged['QString'].connect(self.yoyo)
    self.ComboBox_1.currentIndexChanged['QString'].connect(self.yoyo)
    self.dodo = {"CAR KIT","RING"}
     
    def yoyo(self, txt):
         self.SpinBox_1.setValue(1)
         self.SpinBox_2.setValue(2)
         self.SpinBox_3.setValue(3)
         self.SpinBox_4.setValue(4)
         self.SpinBox_5.setValue(5)
         self.SpinBox_6.setValue(6)
    #--------------------------------------------
    self.ComboBox_0.currentIndexChanged['QString'].connect(self.toto)
    self.ComboBox_1.currentIndexChanged['QString'].connect(self.toto)
    self.dada = {"RSM","RING"}
     
    def toto(self, txt):
         self.SpinBox_1.setValue(6)
         self.SpinBox_2.setValue(6)
         self.SpinBox_3.setValue(6)
         self.SpinBox_4.setValue(6)
         self.SpinBox_5.setValue(6)
         self.SpinBox_6.setValue(6)
    #--------------------------------------------
    self.ComboBox_0.currentIndexChanged['QString'].connect(self.lol)
    self.ComboBox_1.currentIndexChanged['QString'].connect(self.lol)
    self.hello = {"HANDFREE","BEEP"}
     
    def lol(self, txt):
         self.SpinBox_1.setValue(6)
         self.SpinBox_2.setValue(8)
         self.SpinBox_3.setValue(8)
         self.SpinBox_4.setValue(8)
         self.SpinBox_5.setValue(8)
         self.SpinBox_6.setValue(8)
    Merci d'avance.

  12. #12
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Et bien quand ta fonction changed est appelée les spinbox sont mises à jour avec les valeurs que tu leur passes.

    Quel est le problème ?

  13. #13
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Oui il remplie les Spinbox, mais c'est dans l'ordre chronologique.
    Et le self.data = {"HANDFREE","RING"}, prend en compte juste le premier paramètre.

    Voici toute les combinaison possible, et tu va voir le problème.

    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.changed)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.changed)
            self.data = {"HANDSET","VOICE"}
     
        def changed(self, txt):
            self.SpinBox_1.setValue(1)
            self.SpinBox_2.setValue(1)
            self.SpinBox_3.setValue(1)
            self.SpinBox_4.setValue(1)
            self.SpinBox_5.setValue(1)
            self.SpinBox_6.setValue(1)
     
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.tata)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.tata)
            self.data = {"HANDFREE","VOICE"}
     
        def tata(self, txt):
            self.SpinBox_1.setValue(2)
            self.SpinBox_2.setValue(2)
            self.SpinBox_3.setValue(2)
            self.SpinBox_4.setValue(2)
            self.SpinBox_5.setValue(2)
            self.SpinBox_6.setValue(2)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.yoyo)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.yoyo)
            self.dodo = {"CAR KIT","VOICE"}
     
        def yoyo(self, txt):
            self.SpinBox_1.setValue(3)
            self.SpinBox_2.setValue(3)
            self.SpinBox_3.setValue(3)
            self.SpinBox_4.setValue(3)
            self.SpinBox_5.setValue(3)
            self.SpinBox_6.setValue(3)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.toto)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.toto)
            self.dada = {"RSM","VOICE"}
     
        def toto(self, txt):
            self.SpinBox_1.setValue(4)
            self.SpinBox_2.setValue(4)
            self.SpinBox_3.setValue(4)
            self.SpinBox_4.setValue(4)
            self.SpinBox_5.setValue(4)
            self.SpinBox_6.setValue(4)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lol)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lol)
            self.hello = {"HANDSET","RING"}
     
        def lol(self, txt):
            self.SpinBox_1.setValue(5)
            self.SpinBox_2.setValue(5)
            self.SpinBox_3.setValue(5)
            self.SpinBox_4.setValue(5)
            self.SpinBox_5.setValue(5)
            self.SpinBox_6.setValue(5)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lolo)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lolo)
            self.hello = {"HANDFREE","RING"}
     
        def lolo(self, txt):
            self.SpinBox_1.setValue(6)
            self.SpinBox_2.setValue(6)
            self.SpinBox_3.setValue(6)
            self.SpinBox_4.setValue(6)
            self.SpinBox_5.setValue(6)
            self.SpinBox_6.setValue(6)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lola)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lola)
            self.hello = {"CAR KIT","RING"}
     
        def lola(self, txt):
            self.SpinBox_1.setValue(7)
            self.SpinBox_2.setValue(7)
            self.SpinBox_3.setValue(7)
            self.SpinBox_4.setValue(7)
            self.SpinBox_5.setValue(7)
            self.SpinBox_6.setValue(7)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.loli)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.loli)
            self.hello = {"RSM","RING"}
     
        def loli(self, txt):
            self.SpinBox_1.setValue(8)
            self.SpinBox_2.setValue(8)
            self.SpinBox_3.setValue(8)
            self.SpinBox_4.setValue(8)
            self.SpinBox_5.setValue(8)
            self.SpinBox_6.setValue(8)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lolz)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lolz)
            self.hello = {"HANDSET","BEEP"}
     
        def lolz(self, txt):
            self.SpinBox_1.setValue(9)
            self.SpinBox_2.setValue(9)
            self.SpinBox_3.setValue(9)
            self.SpinBox_4.setValue(9)
            self.SpinBox_5.setValue(9)
            self.SpinBox_6.setValue(9)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lolx)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lolx)
            self.hello = {"HANDFREE","BEEP"}
     
        def lolx(self, txt):
            self.SpinBox_1.setValue(10)
            self.SpinBox_2.setValue(10)
            self.SpinBox_3.setValue(10)
            self.SpinBox_4.setValue(10)
            self.SpinBox_5.setValue(10)
            self.SpinBox_6.setValue(10)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lolq)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lolq)
            self.hello = {"CAR KIT","BEEP"}
     
        def lolq(self, txt):
            self.SpinBox_1.setValue(11)
            self.SpinBox_2.setValue(11)
            self.SpinBox_3.setValue(11)
            self.SpinBox_4.setValue(11)
            self.SpinBox_5.setValue(11)
            self.SpinBox_6.setValue(11)
    #--------------------------------------------
            self.ComboBox_0.currentIndexChanged['QString'].connect(self.lole)
            self.ComboBox_1.currentIndexChanged['QString'].connect(self.lole)
            self.hello = {"RSM","BEEP"}
     
        def lole(self, txt):
            self.SpinBox_1.setValue(12)
            self.SpinBox_2.setValue(12)
            self.SpinBox_3.setValue(12)
            self.SpinBox_4.setValue(12)
            self.SpinBox_5.setValue(12)
            self.SpinBox_6.setValue(12)
    Merci d'avance

  14. #14
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    J'ai du mal à saisir ce que tu veux faire en finale.

    Si les valeurs des spinbox sont liées aux textes des combo alors il faut créer un dictionnaire avec les textes comme clés et les valeurs comme ... valeurs.

    Au passage, ton self.hello n'est pas un dictionnaire mais un set.

  15. #15
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Un exemple au cas ou:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    spinvalues = {'Alpha': [2, 5], 'Beta': [4, 12]}
    combobox.currentIndexChanged['QString'].connect(changed)
     
    def changed(text):
        # d'abord convertir la QString en str (ou unicode)
        text = str(text)
        spinbox_1.setValue(spinvalues[text][0]
        spinbox_2.setValue(spinvalues[text][1]

  16. #16
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Bon je croit que, je me fait pas comprendre.

    Au final, ta deux comboBox :
    Dans le comboBox1 tu a : choix1 et choix2
    Dans le comboBox2 tu a : choix3 et choix4

    Et dans ta Spinbox, tu affiche la valeur 4 quand tu choisit "choix1 + choix3"
    et 6 quand tu choisit "choix2 + choix4"

    Montre moi, comment tu fait cela.

    Merci d'avance

  17. #17
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Voici ma version, mais l'erreur est la suivante : global name 'self' is not defined

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    spinvalues = {'HANDSET': [2, 5], 'VOICE': [4, 12]}
    self.ComboBox_0.currentIndexChanged['QString'].connect(self.changed)
    self.ComboBox_1.currentIndexChanged['QString'].connect(self.changed)
     
    def changed(text):
     
            text = str(text)
            self.SpinBox_1.setValue(spinvalues[text][0])
            self.SpinBox_2.setValue(spinvalues[text][1])

  18. #18
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Pour ça tes combo ne doivent pas transmettre le texte.

    [code]
    self.ComboBox_0.currentIndexChanged.connect(self.changed)
    self.ComboBox_1.currentIndexChanged.connect(self.changed)
    [/code

    et dans la fonction:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    def changed_0(self, idx):
        first = strself.(ComboBox_0.currentText())
        other = str(self.ComboBox_1.currentText())
        if first == "choix_1":
            if other == "choix_3":
                value = 4
        elif first == "choix_2":
            if other == "choix_4":
                value = 6
        self.SpinBox_1.setValue(value)
        self.SpinBox_2.setValue(value)
    Donc tu lis le contenu des combo, tu compares avec ce que tu attend et tu fixes les valeurs des spinbox.

  19. #19
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2014
    Messages : 24
    Points : 10
    Points
    10
    Par défaut
    Rebonjour, si je reprend ton code de la semaine dernière, je n'arrive pas a s’insérer une valeur dans un nouveau Spinbox.

    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
     
     
    # -*- coding: utf-8 -*-
    import sys
    from PyQt4.QtCore import Qt
    from PyQt4.QtGui import (QApplication, QWidget, QVBoxLayout, QSpinBox, QComboBox)
     
    class Widget(QWidget):
        def __init__(self):
            super(Widget, self).__init__()
            self.layout = QVBoxLayout(self)
            self.spin = QSpinBox(self)
            self.spin2 = QSpinBox(self)
            self.spin3 = QSpinBox(self)
     
            self.combo = QComboBox(self)
            self.combo2 = QComboBox(self)
     
            self.layout.addWidget(self.spin)
            self.layout.addWidget(self.spin2)
            self.layout.addWidget(self.spin3)
            self.layout.addWidget(self.combo)
            self.layout.addWidget(self.combo2)
     
            self.combo.currentIndexChanged['QString'].connect(self.on_combo_changed)
            self.combo2.currentIndexChanged['QString'].connect(self.changed)
     
     
            self.data = {"HANDSET1": 5,"HANDSET": 6, "HANDFREE": 10, "CAR KIT": 15, "RSM": 20}
            self.dodo = {"HANDSET1": 5, "HANDSET": 76, "HANDFREE": 1, "CAR KIT": 7, "RSM": 0}
     
            self.combo.addItems(self.data.keys())
            self.combo2.addItems(self.dodo.keys())      
     
        def on_combo_changed(self, txt):
     
            self.spin.setValue(self.data[unicode(txt)])
        def changed(self, txt):
            self.spin2.setValue(self.dodo[unicode(txt)])
     
     
     
    if __name__ == '__main__':
        app = QApplication([])
        w = Widget()
        w.show()
        sys.exit(app.exec_())

Discussions similaires

  1. suppression d'un pointeur sur un objet dans un vector
    Par Mindiell dans le forum SL & STL
    Réponses: 9
    Dernier message: 07/08/2008, 14h42
  2. [débutant] tester un pointeur sur un objet
    Par hogan dans le forum C++
    Réponses: 26
    Dernier message: 30/03/2007, 10h02
  3. utilisations de pointeurs sur des objets
    Par niarkyzator dans le forum Delphi
    Réponses: 21
    Dernier message: 13/12/2006, 09h42
  4. [Ada] Récupérer un pointeur sur un objet existant
    Par vincnet500 dans le forum Ada
    Réponses: 1
    Dernier message: 14/11/2004, 14h26
  5. vector de pointeurs sur des objet
    Par jean-bobby dans le forum SL & STL
    Réponses: 26
    Dernier message: 06/08/2004, 14h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo