Search code examples
pythonuser-interfacepyqtqcombobox

PyQt QComboBox addItems not repainted


I have GUI application that plots data from a dump file as various types of plots.A part of the code is given below which sets some dataset values for the type of plot chosen.

Please look a the following code.

def Plottype_menu_event(self) :

    """
    Select plot type as that currently being displayed in the plot type menu and modify the displayed data set menus accordingly.


    Available plot types as listed in the menu
    0 = time trace
    1 = profile
    2 = surface1
    3 = surface2
    4 = contour
    5 = spectrum
    6 = snake
    7 = Power Spectrum surface
    8 = Fundamental Power

    """

    self.nc[0] = self.plotlist.currentIndex()
    text = self.plotlist.currentText()
    print text, self.nc[0]

    """
    Decide whether it is sensible to allow a movie to be made form this plot type (0 = no, 1 = yes), and turn off/on the movie button accordingly.
    """

    movie_allowed = [0, 1, 0, 0, 1, 1, 0, 0, 0]


    """
    Reset dataset to use to the first in the list.
    """
    self.nc[2] = 0

    """
    Update menu of the available datasets in the DATASET_MENU1 and DATASET_MENU2

    Data array to use for each plot type is stored in nc[1]
    1 = tvar1 (tnam1)
    2 = timar (surft)
    3 = tvar3 (surft)
    4 = tvar3 (tnam3)

    """

    if self.nc[0] == 0 :
        self.nc[1] = 4
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam3)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_2_list.clear()
        self.dataset_2_list.addItems(self.tnam3)
        self.dataset_2_list.setCurrentIndex(0)


    """
    Note that profile plots can use either tvar1 or timar

    """

    if self.nc[0] == 1 :
        self.nc[1] = 1
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam1)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_2_list.clear()
        self.dataset_2_list.addItems(self.surft)
        self.dataset_2_list.setCurrentIndex(1)



    if self.nc[0] == 2 :
        self.nc[1] = 1
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam1)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.tnam1)
        self.dataset_2_list.setCurrentIndex(1)


    if self.nc[0] == 3 :
        self.nc[1] = 2
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.surft)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.surft)
        self.dataset_2_list.setCurrentIndex(1)


    if self.nc[0] == 4 :
        self.nc[1] = 3
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam2)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.tnam2)
        self.dataset_2_list.setCurrentIndex(1)


    if self.nc[0] == 5 :
        self.nc[1] = 3
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam2)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.tnam2)
        self.dataset_2_list.setCurrentIndex(1)


    if self.nc[0] == 6 :
        self.nc[1] = 3
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam2)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.tnam2)
        self.dataset_2_list.setCurrentIndex(1)


    if self.nc[0] == 7 :
        self.nc[1] = 1
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam1)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.surft)
        self.dataset_2_list.setCurrentIndex(0)


    if self.nc[0] == 8 :
        self.nc[1] = 1
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam1)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.surft)
        self.dataset_2_list.setCurrentIndex(0)

The variable nc[0] is a switch which reads the user selection from the dropdown menu and updates a set of QComboBox widgets with some select datasets.But for values above nc[0] == 1, the QComboBox have their values changed but they are not painted as such (they have no values) or previous selection's values.The underlying code for the GUI is working perfectly, but the datasets are not updated(ie not repainted with the new values)

I need some help here.Any idea why such a behaviour ?

All help is very much appreciated.


Solution

  • For values of nc[0] greater than 1, the if block that handles the case clears the self.dataset_1_list twice. The second time, it should be acting on self.dataset_2_list

    Eg

    if self.nc[0] == 2 :
        self.nc[1] = 1
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam1)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_1_list.clear()
        self.dataset_2_list.addItems(self.tnam1)
        self.dataset_2_list.setCurrentIndex(1)
    

    should become

    if self.nc[0] == 2 :
        self.nc[1] = 1
        self.dataset_1_list.clear()
        self.dataset_1_list.addItems(self.tnam1)
        self.dataset_1_list.setCurrentIndex(0)
        self.dataset_2_list.clear()                # This line is changed
        self.dataset_2_list.addItems(self.tnam1)
        self.dataset_2_list.setCurrentIndex(1)
    

    This fix needs to applied to most of the blocks of code.

    I would suggest refactoring your code to avoid such issues in the future. For instance, you could do something like:

    comboboxes = [self.dataset_1_list, self.dataset_2_list]
    items_to_add = []
    indices_to_set = []
    if nc[0] == 1:
        self.nc[1] = 1
        items_to_add.append(self.tnam1)
        indices_to_set.append(0)
        items_to_add.append(self.surft)
        indices_to_set.append(1)
    elif nc[1] == 2:
        # etc
    
    for combobox, items, index in zip(comboboxes, items_to_add, indices_to_set):
        combobox.clear()
        combobox.addItems(items)
        combobox.setCurrentIndex(index)
    

    That way you avoid errors from copy/pasting (which is how I assume these problems arose). The same code is used to modify all of the comboboxes, so a bug will affect all of the equally. This should be far easier for you to diagnose yourself if it ever occurs.