Get all tabs widgets in QTabWidget
Asked Answered
B

2

25

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.

Berlioz answered 29/5, 2011 at 10:31 Comment(0)
I
41

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.

Inexpiable answered 29/5, 2011 at 10:35 Comment(2)
Yep, it just hides at the very end.Berlioz
Example: for (int i = 0; i < ui->yourTabWidget->count(); i++) { auto widget = ui->yourTabWidget->widget(i); }Leviathan
V
0

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()
Vaivode answered 23/9, 2023 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.