How to make a tabbar last one ???

  • hi I have a QTabBar and i'd like to have a tab that should be like the last tab in a Browser (the addTab "+") but I don't know how to make it the last one (especially when the others move )
    thanks for your help :)

  • There is another way, but I think "this": should do the trick too. If not, I'll post my version tomorrow if I don't forget.

  • thank you HuXiKa but i think that you didn't understand my question i'd like to have this

  • I understood, and I have a solution, I'll post it :) Although, in my solution the "+" button is the first tab, but it shouldn't be hard to change it.

  • If you use QTabWidget, you can consider this:

    void QTabWidget::setCornerWidget ( QWidget * widget, Qt::Corner corner = Qt::TopRightCorner )

  • Thank you very much HuXiKa
    1+1=2 : setCornerWidget is not a solution because the widget will be in the top right in the corner not near other tabs

  • So, as I'm looking at it, my solution is what 1 + 1 = 2 said (and even more complicated, because I subclassed QTabWidget to have editable tab names):

    QToolButton *newTabButton = new QToolButton(this);
    setCornerWidget(newTabButton, Qt::TopLeftCorner);
    QObject::connect(newTabButton, SIGNAL(clicked()), this, SLOT(addNewTab()));
    newTabButton->setToolTip(tr("New Tab"));

    And the important part of addNewTab slot is:

    void EditableTabWidget::addNewTab()
    QWidget *widget = new QWidget(this);

    The difference here is that the button is in the top left corner, so it's not that far from the tabs.
    If I remember correctly when I tried to solve this problem this was the easiest way, it's complicated to achive the behavior that you want. But google is your friend, try to look for a solution :)

  • yes I tried this code but the problem is that the button is on the left side and if we change
    @setCornerWidget(newTabButton, Qt::TopLeftCorner);@ to @setCornerWidget(newTabButton, Qt::TopRightCorner);@ it will be in top right so I don't think that is a solution
    but i'm trying some codes and like this

    @MytabWidget::MytabWidget() : QTabWidget
    QWidget *wid1 = new QWidget;
    QWidget *wid2 = new QWidget;
    addTab(wid1, "wid1");
    addTab(wid2, "+");
    tabBar()->setTabButton(1, QTabBar::RightSide, 0);

    connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
            connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(fermerautreonglet(int)));


    @void MytabWidget::tabChanged(int idx)
    if (idx == count()-1)
    QWidget *wid3 = new QWidget;
    insertTab(count()-1, wid3, "new tab");
    void MytabWidget::fermerautreonglet(int index)
    if (count() > 1)
    I think that this this code is the best but my problem is when tabs move the "+" tab will lose the last position but i think there is a class that can help us to resolve this problem wich is "QStyleOptionTab":
    in Public Variables we have TabPosition position i think that this can do it
    but I'm a newbie and i didn't know how to use it i need your help! thanks

  • In my project i have some model on the basic of witch i do reinit a tabBar (delete all tabs, and create them again), then if my model talk me what in model no one item to visualize, i create an "empty" tab (like in browser), and at end, i create "Add" tab. And i have connect with TabBar and my class, and when user push at some of tabs, i get a signal, then in connected with this signal slot's i analyze if this tab bar is "Add", then create a new item at model.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.