【问题标题】:Why is QTabBar giving me the wrong index number?为什么 QTabBar 给我错误的索引号?
【发布时间】:2023-03-23 18:37:01
【问题描述】:

所以我在标签栏中有两个标签。我设置了一个信号,以便在单击时运行一个函数。该函数调用当前索引并打印它。

为什么打印出来的页面的索引是它所在但当前不在的页面?

import sys
from PySide2.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.tabs = QTabBar()
        self.tabs.addTab("Main Menu")
        self.tabs.addTab("Network Menu")

        layout = QHBoxLayout()
        layout.addWidget(self.tabs)

        self.tabs.tabBarClicked.connect(self.tab_push)
        self.setLayout(layout)

    def tab_push(self):
        x = self.tabs.currentIndex()
        print(x)

app = QApplication(sys.argv)

window = Window()
window.show()

sys.exit(app.exec_())

缩进有点奇怪,不知道为什么会这样,但它仍然有效。

【问题讨论】:

    标签: python python-3.x pyqt5 pyside2


    【解决方案1】:

    这不是错误,而是正常行为:当按下 tabbar 时,获取索引,发出信号 tabBarClicked,然后更改 currentIndex,然后在发出信号和更新 currectIndex 之间有一段时间,就是在那一刻,你给人留下了深刻的印象。如果您想获取按下的选项卡的索引,请使用信号信息:

    def tab_push(self, index):
        print(index)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多