Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] setCurrentIndex not working correctly

[SOLVED] setCurrentIndex not working correctly

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.7k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    kdev
    wrote on last edited by kdev
    #1

    Hi,
    I'm trying to change the focused tab inside a QTabWidget after clicking the latest one using that code:
    tabwidget->setCurrentIndex(this->get_oldtabindex(tabwidget));
    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.
    Thanks.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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)
              w->setCurrentIndex(lastIndex);
          else
              lastIndex = index;
      });
      
      K 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        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)
                w->setCurrentIndex(lastIndex);
            else
                lastIndex = index;
        });
        
        K Offline
        K Offline
        kdev
        wrote on last edited by kdev
        #3

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

        1 Reply Last reply
        0

        • Login

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