Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Tabs not appearing; what am I doing wrong?

Tabs not appearing; what am I doing wrong?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    space_mogul
    wrote on last edited by space_mogul
    #1

    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 the empire_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")
    
    
    JonBJ 1 Reply Last reply
    0
    • S space_mogul

      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 the empire_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")
      
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @space_mogul I don't see anywhere you add self.top_level_tabs onto your dialog.

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @space_mogul I don't see anywhere you add self.top_level_tabs onto your dialog.

        S Offline
        S Offline
        space_mogul
        wrote on last edited by
        #3

        @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?

        JonBJ 1 Reply Last reply
        0
        • S space_mogul

          @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?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @space_mogul
          QMainWindow (or for that matter QDialog) is a QWidget 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 onto self (your dialog?) because of the parameter. It will be seen, but with no layout will appear at top-left. But self.top_level_tabs = QTabWidget() does not specify self 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 a centralWidget, see https://doc.qt.io/qt-6/qmainwindow.html#qt-main-window-framework. If you want your QTabWidget to appear there add it (on a layout) to that. Having said this, nothing in your code actually seems to show that you have a QMainWindow.

          And btw when you get this sorted out your show() issue should go away.

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @space_mogul
            QMainWindow (or for that matter QDialog) is a QWidget 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 onto self (your dialog?) because of the parameter. It will be seen, but with no layout will appear at top-left. But self.top_level_tabs = QTabWidget() does not specify self 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 a centralWidget, see https://doc.qt.io/qt-6/qmainwindow.html#qt-main-window-framework. If you want your QTabWidget to appear there add it (on a layout) to that. Having said this, nothing in your code actually seems to show that you have a QMainWindow.

            And btw when you get this sorted out your show() issue should go away.

            S Offline
            S Offline
            space_mogul
            wrote on last edited by
            #5

            @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()
            
            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved