Search code examples
pythonpyqtpyqt4qcomboboxqt-signals

Pyqt4 Qcombobox signal is not firing upon user input but it does when done with .setCurrentIndex


The QComboBox currentIndexChanged Signal is not firing when a new item is selected from user. But it does fire when .setCurrentIndex is used within the code. (line 91 and 92).

I have a QTabWidget. In tab1 I have a Qvbox into which three Qhboxes are added. Each Qhbox is from the class Kaskade and contains two widgets, a QCombobox and a QstackedWidget. Depending of the current Index of the QCombobox the QstackWidget will either show a QLCD number or a Qspinbox. If the user changes the QCombobox index in the GUI the currentIndexChanged Signal is not emitted, although the QCombobox shows the new item.

What am I missing? Any kind of help is appreciated.

This is my test code

# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

import sys



class Kaskade(QtGui.QWidget):
   def __init__(self,sp,sp_min,sp_max, parent = None):
      super(Kaskade, self).__init__(parent)


      self._sp=sp
      self._sp_min=sp_min
      self._sp_max=sp_max


      self.sp()
      self.hbox_gen()


   def mode_changed(self,i):
      print "Mode has changed to", i
      self.sp_stack.setCurrentIndex(i)


   def sp(self):
      self.sp_stack=QtGui.QStackedWidget(self)
      self.sp1 = QtGui.QWidget()
      self.sp2 = QtGui.QWidget()

      self.sp1UI()
      self.sp2UI()

      self.sp_stack.addWidget(self.sp1)
      self.sp_stack.addWidget(self.sp2)

   def sp1UI(self):
      self.sp1_layout=QtGui.QHBoxLayout()
      self.sp1_lcd=QtGui.QLCDNumber(self)
      self.sp1_layout.addWidget(self.sp1_lcd)
      #self.sp1.connect(lcd_pv.display)
      self.sp1.setLayout(self.sp1_layout)   

   def sp2UI(self):
      self.sp2_layout=QtGui.QHBoxLayout()
      self.sp2_spinBox=QtGui.QSpinBox()
      self.sp2_spinBox.setRange(self._sp_min,self._sp_max)
      self.sp2_spinBox.setValue(self._sp)
      self.sp2_layout.addWidget(self.sp2_spinBox)
      self.sp2.setLayout(self.sp2_layout)   


   def hbox_gen(self):

      self.mode=QtGui.QComboBox(self)
      self.mode.addItem("OFF")
      self.mode.addItem("ON")
      self.mode.currentIndexChanged.connect(self.mode_changed)

      self.hbox = QtGui.QHBoxLayout()
      self.hbox.addWidget(self.mode)
      self.hbox.addWidget(self.sp_stack)



class tabdemo(QtGui.QTabWidget):
   def __init__(self, parent = None):
      super(tabdemo, self).__init__(parent)
      self.tab1 = QtGui.QWidget()
      self.tab2 = QtGui.QWidget()
      self.tab3 = QtGui.QWidget()

      self.addTab(self.tab1,"Tab 1")
      self.addTab(self.tab2,"Tab 2")
      self.addTab(self.tab3,"Tab 3")
      self.tab1UI()
      self.tab2UI()
      self.tab3UI()
      self.setWindowTitle("Heizung")


   def tab1UI(self):


      K1=Kaskade(28,5,40)
      K2=Kaskade(30,5,40)
      K3=Kaskade(35,5,40)

      K1.mode.setCurrentIndex(1)
      K3.mode.setCurrentIndex(1)

      vbox = QtGui.QVBoxLayout()
      vbox.addLayout(K1.hbox)
      vbox.addLayout(K2.hbox)
      vbox.addLayout(K3.hbox)
      self.tab1.setLayout(vbox)

      self.setTabText(1,"Tab1")

   def tab2UI(self):
      self.setTabText(1,"Tab2")


   def tab3UI(self):
      self.setTabText(2,"Tab3")








def main():

    app = QtGui.QApplication(sys.argv)
    ex = tabdemo()
    ex.show()


    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Solution

  • You must add Kaskade to vbox in Tab 1, not the layout. In addition we must do self.hbox layout of Kaskade:

    class Kaskade(QtGui.QWidget):
       [...]
       def hbox_gen(self):
          [...]
          self.hbox = QtGui.QHBoxLayout(self)
          [...]
    
    
    class tabdemo(QtGui.QTabWidget):
       [...]
       def tab1UI(self):
          [...]
          vbox = QtGui.QVBoxLayout()
          vbox.addWidget(K1)
          vbox.addWidget(K2)
          vbox.addWidget(K3)
          self.tab1.setLayout(vbox)
          [...]