Qiao
Qiao

Reputation: 17049

Get all tabs widgets in QTabWidget

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.

Upvotes: 25

Views: 29326

Answers (2)

rllm
rllm

Reputation: 11

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()

Upvotes: 1

Mat
Mat

Reputation: 206669

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.

Upvotes: 41

Related Questions