Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved] QTabWidget:setTabBar() -- really undefined behavior?

    General and Desktop
    3
    5
    1883
    Loading More Posts
    • 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.
    • D
      dcortesi last edited by

      I would like to completely repopulate a QTabWidget with a different group of tabs, quickly and simply.

      I cleverly thought, aha! I could maintain two different QTabBars, each populated with a group of widgets. Then use QTabWidget::setTabBar() to change from one group of tabs to the other.

      Alas I see "the doc for setTabBar()":http://qt-project.org/doc/qt-5/qtabwidget.html#setTabBar says "Note that this must be called before any tabs have been added, or the behavior is undefined."

      Does this really mean one cannot swap loaded tab bars in a tab widget? Could someone familiar with the actual code tell me what will go wrong? Thank you!

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        Best way is to try it yourself, and see what happens. When the devs say "it's undefined" it usually means "untested, might crash", and it always does mean "if it works, great. If it doesn't: well, you have been warned".

        I'll hint at another solution: why not swap the whole QTabWidget, instead of swapping only the tabs?

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • D
          dcortesi last edited by

          Thanks! I guess I will take the time to write a test case. I thought of swapping the QTabWidget itself, but that's awkward because it is one side of a QSplitter, and QSplitter doesn't seem to offer any method for removing a widget.

          I'll report here on what happens!

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Then QStackedWidget to the rescue, you only switch the current tab widget and don't have to fiddle with QSplitter

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • D
              dcortesi last edited by

              OK, what happens is ... I realize my mistake! Because QTabBar only manages the tabs, not the related widgets. So it would do me no good to swap populated tab bars, that would only change the names on the tabs and perhaps their order.

              QTabWidget manages the relationship between each tab and an associated widget, displaying the latter when the former is clicked. I can well imagine that these relationships could be scrambled in "undefined" ways if I changed the tab bar after widgets were added.

              I will look into QStackedWidget and see if it fits into my app. Thanks for your help.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post