Nominate our 2022 Qt Champions!

[SOLVED] Allowing widgets contained in a QTabWidget to see index

  • Hello,

    I was wondering if it is possible to allow a widget within a QTabWidget to see the index number of the tab its in. Here is a piece of code that shows what I am trying to do:

    tabWidget->addTab(new browseTab(this, "", ###) ,tr("Browser"));

    My idea is this:

    1. when a new tab is added, create a new instance of the class browseTab.
    2. declare the parent, url, and index number in the browseTab class constructor.

    I have had success passing the URL on to browseTab in this manner, but I'm not sure how to send the index number. The index number should replace the ### symbols obviously. How can I pass the new tab's index number to the widget (browseTab) contained therein?

    Any help would be appreciated!

  • Lifetime Qt Champion


    Use insertTab so you set the index yourself. Take a look at the documentation to see how it works, specially if you pass an out of range index.

  • Sgaist,

    My program allows new tabs to be added dynamically (web browser). The tabs are movable, so you can drag and drop them with impunity. Since my last post, I've come up with a new system:

    1. When a new tab is created, the associate page is given a unique, random ID#.
    2. This ID# is saved into a QList. This QList associates an ID# with an index #. ie "791" is located at position 2 in the QList. 791 is the ID, 2 is the index.

    Heres the problem. When you linearly add tabs one after another, the QList increments in a predictable way: assigning a new index to each new ID. If I move a tab around, the QList doesn't update to move the ID to the correct index. Is there a way to use insertTab along with the drag-drop tabs? I don't think my system is the most efficient.

    Thanks for your help!

  • I have solved the problem!

Log in to reply