【问题标题】:PyQt QComboBox addItems not repaintedPyQt QComboBox addItems 未重绘
【发布时间】:2015-06-10 19:08:20
【问题描述】:

我有 GUI 应用程序,可以将转储文件中的数据绘制为各种类型的图。下面给出了部分代码,它为所选的图类型设置了一些数据集值。

请看下面的代码。

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)

变量nc[0]是一个开关,它从下拉菜单中读取用户选择,并使用一些选择数据集更新一组QComboBox小部件。但是对于nc[0] == 1以上的值,QComboBox的值发生了变化,但它们没有像这样绘制(它们没有值)或先前选择的值。GUI 的底层代码运行良好,但数据集未更新(即未使用新值重新绘制)

我需要一些帮助。知道为什么会出现这种行为吗?

非常感谢所有帮助。

【问题讨论】:

    标签: python user-interface pyqt qcombobox


    【解决方案1】:

    对于大于 1 的 nc[0] 的值,处理这种情况的 if 块会清除 self.dataset_1_list 两次。第二次,应该作用于self.dataset_2_list

    例如

    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] == 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)
    

    此修复需要应用于大多数代码块。

    我建议重构您的代码以避免将来出现此类问题。例如,您可以执行以下操作:

    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)
    

    这样您就可以避免复制/粘贴错误(这是我假设这些问题出现的原因)。相同的代码用于修改所有的组合框,因此一个错误将同样影响所有的组合框。如果发生这种情况,您应该更容易诊断自己。

    【讨论】:

    • 谢谢先生的回复。你对复制粘贴的事情非常正确。最糟糕的是,我看了大约 10 次代码,却没有注意到那个错误。我将尝试学习重构代码的方法。
    • @ShaikJaffer 不用担心。我很高兴能帮上忙。如果我的帖子完全回答了您的问题,请点击左侧的勾号“接受”(如果您不确定如何操作,请参阅this 帖子)
    猜你喜欢
    • 2011-02-10
    • 2011-08-20
    • 2013-12-26
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多