获取QTabWidget中的所有选项卡小部件

发布于 2024-11-10 18:28:35 字数 228 浏览 7 评论 0 原文

是否可以获取 addTab(QWidget() 添加的所有选项卡小部件列表中的“noreferrer”>QTabWidget

我们可以使用self.findChildren(QWidget),但它也会返回其自身内部的所有其他小部件,并且无法过滤它们。

Is it possible to get all tabs widgets that where added by addTab(QWidget() in QTabWidget in a list.

We can use self.findChildren(QWidget), but it also returns all other widgets inside itself, and there is no way to filter them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-11-17 18:28:35

更仔细地阅读您指出的文档:-)

QTabWidget 有一个 QWidget *widget(int index) 方法,返回索引 index 处的选项卡。用它来获取选项卡小部件。该类还有一个 int count(); 来告诉您有多少个选项卡。

有了这两个选项卡,您就可以轻松地遍历所有选项卡。

Read the documentation you pointed to more carefully :-)

QTabWidget has a QWidget *widget(int index) method that returns the tab at index index. Use that to get the tab widgets. That class also has a int count(); that tells you how many tabs there are.

With these two, you can iterate over all the tabs quite easily.

虐人心 2024-11-17 18:28:35

我终于让它发挥作用了。将 setCentralWidget 从 tabs 更改为 tabWidget 纠正了所有错误。选项卡呈现了我在 QtDesiger 中定义的更改后的屏幕。我删除了所有调试代码。这是我修改的代码。

import sys
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6 import QtWidgets
from PyQt6.uic import loadUi
# from PyQt6.QtWidgets import QTabWidget
# from PyQt6.QtCore import pyqtSignal as Signal, pyqtSlot as Slot


class MainUI(QMainWindow):

    def __init__(self):
        super(MainUI, self).__init__()   
        loadUi('D:/virtual1/AI-Project/AI_Main.ui', self)
    #    self.tabs = QTabWidget()
        self.setCentralWidget(self.tabWidget)
        self.tabWidget.currentChanged.connect(self.on_change)

    def on_change(self):
        num = self.tabWidget.currentIndex()
        print('num = ', num)
        if num == 0:
            print('Index 0')
        if num == 1:
            print('Index 1')
        if num == 2:
            print('Index 2')
        if num == 3:
            print('Index 3')


if __name__ == "__main__":
    # Allows You to Execute Code When the File Runs as a Script, but Not When It's Imported as a Module.
    app = QtWidgets.QApplication(sys.argv)
    window = MainUI()
    window.show()
    app.exec()

I have finally got this to work. Altering setCentralWidget from tabs to tabWidget corrected all faults. Tabs presented an altered screen from what I defined in QtDesiger. I deleted all my debugging code. Here is my altered code.

import sys
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6 import QtWidgets
from PyQt6.uic import loadUi
# from PyQt6.QtWidgets import QTabWidget
# from PyQt6.QtCore import pyqtSignal as Signal, pyqtSlot as Slot


class MainUI(QMainWindow):

    def __init__(self):
        super(MainUI, self).__init__()   
        loadUi('D:/virtual1/AI-Project/AI_Main.ui', self)
    #    self.tabs = QTabWidget()
        self.setCentralWidget(self.tabWidget)
        self.tabWidget.currentChanged.connect(self.on_change)

    def on_change(self):
        num = self.tabWidget.currentIndex()
        print('num = ', num)
        if num == 0:
            print('Index 0')
        if num == 1:
            print('Index 1')
        if num == 2:
            print('Index 2')
        if num == 3:
            print('Index 3')


if __name__ == "__main__":
    # Allows You to Execute Code When the File Runs as a Script, but Not When It's Imported as a Module.
    app = QtWidgets.QApplication(sys.argv)
    window = MainUI()
    window.show()
    app.exec()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文