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. How to change the order of tabs in a QTabWidget

How to change the order of tabs in a QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 10.2k Views 1 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.
  • T Tomax63

    Good Morning all

    I want to change the order of my tabs according to the sequence they had when I left the program last time.
    I have already saved the names in the correct order in a string, separated by ";":

    Now I want to bring the tabs into this exact sequence when startung the program. Here is my routine so far:

    tab_sequenz = "definition;digitalisierung;dig2;qhi;anlage;drehmoment;medien;pumpentypauswahl;npsh;isobar;isochor;alt_medium;bypass;drossel;parallel;simulation;tco;einstellungen"
    
    
    void MainWindow::restore_tab_seq(QString tab_sequenz){
        QTabWidget *w;
        QWidget t;
        qint32 i = 0;
        qint32 to;
        foreach (QString v, tab_sequenz.split(";")) {
            cout << v.toStdString() << endl;
            w = ui->my_tabwidget->findChild<QTabWidget *>(v); //get the necessary widget
            to = i;
            if (w != NULL){
                cout << w->currentIndex() << endl;
            }
        }
    }
    

    w appears to be NULL at all times. How can I :

    1. Find the tab with the name saved in tab_sequenz[i]?
    2. send the tab to the position i?

    Thomas

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Tomax63 Why don't you create the tabs in the correct order? Then there is no need to sort them.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tomax63
      wrote on last edited by
      #3

      Because each customer shall be able to save the order of tabs individually.
      th order is saved in the string tab_sequenz when leaving the program and this order can be different from the order in my original ui.

      jsulmJ 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #4

        did you try tabwidget->tabBar()->move(from,to); https://doc.qt.io/qt-5/qtabbar.html?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • T Offline
          T Offline
          Tomax63
          wrote on last edited by
          #5

          Thanks for the tip. But to try this, i first have to find the "from", and I just have the name of the tab.
          This is why I try to find its index.

          After I found the actuel position, i will move it by using the move() - instruction.

          1 Reply Last reply
          0
          • T Tomax63

            Because each customer shall be able to save the order of tabs individually.
            th order is saved in the string tab_sequenz when leaving the program and this order can be different from the order in my original ui.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Tomax63 said in How to change the order of tabs in a QTabWidget:

            Because each customer shall be able to save the order of tabs individually.

            That's clear. But how do you create the tabs? I mean, you could create them dynamically when the app starts and do it in the order which was saved.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tomax63
              wrote on last edited by
              #7

              I create them in QTDesigner

              How can I create them again during app start?

              JonBJ 1 Reply Last reply
              0
              • T Tomax63

                I create them in QTDesigner

                How can I create them again during app start?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #8

                @Tomax63

                1. Let them be created initially in the order defined in Qt Creator. this is good anyway for first time, when there is no saved order.
                2. Read the saved order, if any.
                3. Move them around accordingly.

                What is wrong/the actual problem with this?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tomax63
                  wrote on last edited by
                  #9

                  Your point 3 is the problem. I dont know how to do it.

                  JonBJ 1 Reply Last reply
                  0
                  • T Tomax63

                    Your point 3 is the problem. I dont know how to do it.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #10

                    @Tomax63
                    As @VRonin said, you will use tabwidget->tabBar()->move(from,to). Is there something specific about that which you are not able to do?

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      Tomax63
                      wrote on last edited by
                      #11

                      Yes, i want to find the "from" value.

                      jsulmJ JonBJ 3 Replies Last reply
                      0
                      • T Tomax63

                        Yes, i want to find the "from" value.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Tomax63 I guess you need to iterate over all these widgets until you find the one with given text?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • T Tomax63

                          Yes, i want to find the "from" value.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #13

                          @Tomax63
                          So you'd like us to look at your original code and figure why it can't find the tab, right?

                          I'll look in a sec. Meanwhile:

                          qint32 i = 0;
                          ...
                          to = i;
                          

                          How is to ever going to be anything other than 0?

                          Although @VRonin wrote tabwidget->tabBar()->move(from,to);, I only actually see void QTabBar::moveTab(int from, int to) (https://doc.qt.io/qt-5/qtabbar.html#moveTab). from & to are integer positions, so do yourself a favor and start by replacing your foreach (QString v, tab_sequenz.split(";")) with an old-fashioned for loop counter so that it's easy to get number positions....

                          1 Reply Last reply
                          3
                          • T Tomax63

                            Yes, i want to find the "from" value.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #14

                            @Tomax63
                            So I think you are saying your

                             w = ui->my_tabwidget->findChild<QTabWidget *>(v); //get the necessary widget
                            

                            is not finding the tab by name. That would imply one of:

                            • The name is not right.
                            • The matching tab is not currently in the widget.

                            So do a little digging. What tabs are actually there? What are their names and what are you searching for? If necessary, just do the iteration in your own code to see what's going on. In any case, you're going to need the tab's index to pass to move(), so from findChild() you're going to have re-look up the result in the tabs, you might be best off doing it yourself retuning an index number in the first place....

                            1 Reply Last reply
                            3
                            • T Offline
                              T Offline
                              Tomax63
                              wrote on last edited by Tomax63
                              #15

                              I made it.

                              void MainWindow::restore_tab_seq(QString tab_sequenz){
                                  QStringList v = tab_sequenz.split(";");
                                  qint32 i = 0, j = 0, k = v.count();
                                  for(i=0; i<k; i++){
                                      for (j=0; j<ui->registerkarten->tabBar()->count(); j++){
                                          if (ui->registerkarten->tabBar()->tabText(j).toStdString() == v[i].toStdString()){
                                              ui->registerkarten->tabBar()->moveTab(j, i);
                                          }
                                      }
                                  }
                              }
                              
                              
                              Thanks for your help, mates.
                              
                              1 Reply Last reply
                              1
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                Hi,

                                Why are you using toStdString to compare two QStrings ?

                                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
                                1

                                • Login

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