Important: Please read the Qt Code of Conduct -

[SOLVED] setCurrentIndex not working correctly

  • Hi,
    I'm trying to change the focused tab inside a QTabWidget after clicking the latest one using that code:
    While I'm sure that this->get_oldtabindex(tabwidget) always returns the correct index of the old focused tab before the click of the latest tab happens, the targeted tab doesn't get selected after calling setCurrentIndex.
    So what's wrong here.

  • Moderators

    Hi, welcome to devnet.
    Are you connecting to currentChanged signal or detecting a click on a tab?

    This works for me:

    auto w = new QTabWidget;
    w->addTab(new QWidget, "First");
    w->addTab(new QWidget, "Second");
    int lastOne = w->addTab(new QWidget, "Last");
    connect(w, &QTabWidget::currentChanged, [=](int index){
        static int lastIndex = 0;
        if(index == lastOne)
            lastIndex = index;

  • @Chris-Kawa I missed that. It works fine now, after using setCurrentIndex inside currentChanged signal's slot. My mistake was using tabBarClicked signal's slot instead: which caused the problem.
    Thanks a lot for your helpful answer.

Log in to reply