Tabs not appearing; what am I doing wrong?
-
Edit: I somewhat figured it out, still need some advice though. If I add this line to the class file:
self.top_level_tabs.show()
Then the tabs will appear, but as a second window immediately behind the first one that's created by calling
empire_viewer.show()
. However, if I remove theempire_viewer.show()
line, nothing appears. So, I'm stuck in a catch-22.Hi. This is probably a newb question, but I'm trying to add some tabs to my PySide6 application. However, the tabs just don't appear. I made sure that setTabBarAutoHide is set to false. I've been messing around with this all day trying various things and it's just not happening. Really could use to help to point out what I'm doing wrong.
# Main file import xml.etree.ElementTree as ET import sys from PySide6.QtWidgets import QApplication, QLabel, QPushButton, QDialog, QLineEdit, QVBoxLayout from PySide6.QtCore import Slot from ui import UI import wx if __name__ == '__main__': app = QApplication(sys.argv) empire_viewer = UI() empire_viewer.show() app.exec()
# Class file from PySide6 import QtCore from PySide6.QtWidgets import QApplication, QMainWindow from PySide6.QtWidgets import QTabWidget, QHBoxLayout from PySide6.QtWidgets import QDialog from PySide6.QtWidgets import QWidget, QPushButton class UI(QDialog): def __init__(self): super().__init__() ship_trade_page = QPushButton("Text", self) self.top_level_tabs = QTabWidget() self.top_level_tabs.setTabBarAutoHide = False ship_trade_layout = QHBoxLayout(ship_trade_page) self.top_level_tabs.addTab(ship_trade_page, "Trades")
-
@space_mogul I don't see anywhere you add
self.top_level_tabs
onto your dialog. -
@JonB - I don't see how to do that. I would assume something like
self.add(top_level_tabs)
, but there's no method in QMainWindow for that.I've tried some more things, but still am not able to get this to work as expected. Is there a better way that I should be doing this?
# Class file ship_trade_btn = QPushButton("Text", self) self.top_level_tabs = QTabWidget() self.top_level_tabs.setTabBarAutoHide = False self.top_level_tabs.addTab(ship_trade_btn, "Trades") self.top_level_tabs.show()
I condensed the code down a bit and this technically gets me what I need. But it's the extra
show()
that seems to screw things up. I would expect that showing the whole QMainWindow object in the main.py file would also show tabs, but the tabs are apparently a totally separate entity? -
@space_mogul
QMainWindow
(or for that matterQDialog
) is aQWidget
like any other. See https://doc.qt.io/qt-6/qwidget.html#top-level-and-child-widgets. You position child widgets on parent widget via layouts, https://doc.qt.io/qt-6/layout.html.As your code stands,
ship_trade_btn = QPushButton("Text", self)
adds the button ontoself
(your dialog?) because of the parameter. It will be seen, but with no layout will appear at top-left. Butself.top_level_tabs = QTabWidget()
does not specifyself
as parent, so it does not belong anywhere and is not related to the dialog.Normally:
parentWidget = QWidget() # `self` is optional here because following code will add it via `parentWidget.setLayout(layout)` layout = QHBoxLayout() # or QVBoxLayout() layout.addWidget(childWidget) parentWidget.setLayout(layout)
In the case of
QMainWindow
it has acentralWidget
, see https://doc.qt.io/qt-6/qmainwindow.html#qt-main-window-framework. If you want yourQTabWidget
to appear there add it (on a layout) to that. Having said this, nothing in your code actually seems to show that you have aQMainWindow
.And btw when you get this sorted out your
show()
issue should go away. -
@JonB - Thanks. I finally got it working. The key was setting the centalWidget to the top_level_tabs widget. Here's the winning code:
ship_trade_btn = QPushButton("Text", self) self.top_level_tabs = QTabWidget() self.top_level_tabs.setTabBarAutoHide = False ship_trade_layout = QHBoxLayout(ship_trade_btn) ship_trade_layout.addWidget(ship_trade_btn) self.setCentralWidget(self.top_level_tabs) self.setLayout(ship_trade_layout) self.top_level_tabs.addTab(ship_trade_btn, "Trades") self.centralWidget().show()