将变量从一类更新到另一类

时间:2019-03-04 18:57:07

标签: python

每当关闭选项卡时,我都试图从QTabWidget类更新在QTabBar类中创建的字典变量。

尽管它似乎正在删除我在QTabWidget类中想要的键,但实际的字典变量似乎在QTabBar类中没有得到更新。

因此,当我尝试在主文件window.py文件中调用该变量时,仍然可以在该变量中找到初始值。

在下面的示例中,如果我从0中删除索引键self.custom_dict,则只希望看到索引键1,但是在我的主文件中,我仍会得到结果两个索引。

我的问题是-变量可以从一个类更新为另一个类吗?还是应该采用全局变量?

window.py

class MyApp(object):
    def __init__(self):
        ...
        ...
        self._setupUI()

    def _setupUI(self):
        ...
        ...
        self._tab_widget = TabWidget()
        my_btn = QtGui.QPushButton('Get info')
        my_btn.clicked.connect(self.get_dict_info)

    def get_dict_info(self):
        custom = self._tab_widget.tab_bar.custom_dict

        # If I run this function, it is returning me both indexes in
        # which I am only expecting key `1` only
        '''
        {
            0: {
                'listA' : ['itemA_01', 'itemA_02'],
                'listB' : ['itemB_01']
            },

            1: {
                'listC' : ['itemC_01', 'itemC_02']
            },

        }
        '''

tabs.py

class TabWidget(QtGui.QTabWidget):
    def __init__(self, parent=None):
        super(TabWidget, self).__init__(parent)
        ...
        ...
        self.setupUI()

    def setupUI(self):
        ...
        ...
        self.tab_bar = TabBar()
        self.tab_bar.tabDeleteRequested.connect(self._removeTab)

    @QtCore.Slot(int)
    def _removeTabFromPrompt(self, index):
        self.removeTab(index)
        custom_dict = self.tabBar().custom_dict
        del custom_dict[index]

        # custom_dict does and will only return me the following:
        '''
        {
        1: {
                'listC' : ['itemC_01', 'itemC_02']
            },

        }
        '''



class TabBar(QtGui.QTabBar):

    tabDeleteRequested = QtCore.Signal(int)

    def __init__(self, parent=None):
        super(TabBar, self).__init__(parent)
        ...
        ...
        self.custom_dict = defaultdict(dict)
        '''
        Example of self.custom_dict:

        {
            0: {
                'listA' : ['itemA_01', 'itemA_02'],
                'listB' : ['itemB_01']
            },

            1: {
                'listC' : ['itemC_01', 'itemC_02']
            },

        }

        '''

    def mousePressEvent(self, event):
        self.index = self.tabAt(event.pos())

        if event.button() == QtCore.Qt.RightButton:
            self._showContextMenu(event.pos(), self.index)
        else:
            super(TabBar, self).mousePressEvent(event)

    def _showContextMenu(self, position, index):
        """Add the right-click menu to the given tab.

        Args:
            position (QtCore.QPoint): Position offset based on tab
                widget coordinates.
            index (int): The tab index.
        """
        role = self.tabData(index)
        self.qmenu = QtGui.QMenu(self)
        self.qmenu.setTitle(self.tabText(index))

        if utils.has_edit_permissions(role):
            remove_icon = QtGui.QIcon(utils.get_icon_path('remove'))
            remove_tab_action = QtGui.QAction(remove_icon, 'Remove Tab', self)
            slot = functools.partial(self.tabDeleteRequested.emit, index)
            remove_tab_action.triggered.connect(slot)
            self.qmenu.addAction(remove_tab_action)

        else:
            message = ('You do not have permission')
            disabled_action = QtGui.QAction(message, self)
            disabled_action.setEnabled(False)
            self.qmenu.addAction(disabled_action)

        global_position = self.mapToGlobal(self.pos())
        self.qmenu.exec_(QtCore.QPoint(
            global_position.x() - self.pos().x() + position.x(),
            global_position.y() - self.pos().y() + position.y()
        ))    

0 个答案:

没有答案