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. QTabWidget set current tab by tab name?
Qt 6.11 is out! See what's new in the release blog

QTabWidget set current tab by tab name?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 9.6k 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.
  • L lansing

    @jsulm

    I have this QTabWidget tied to the QComboBox on another dialog sharing the same tab name. If the tabwidget changed, the combobox will also changed and vise versa. I also have functions that remove and add new items to this tabwidget/combobox as well as allowing reordering of tab, so I can't connect them through the same index, I have to connect them by names.

    I have a slot catching the signal on the combobo change, and in the slot I have retrieved my widget through std::find_if with a lamda expression. But when I use it with indexof() I got the error

    error: cannot initialize a parameter of type 'QWidget *' with an lvalue of type 'QVector<FilterWidget>::iterator' (aka 'FilterWidget*')
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #5

    @lansing Then why not use index from https://doc.qt.io/qt-5/qcombobox.html#activated to activate the tab?

    "But when I use it with indexof() I got the error" - well, I guess (you don't show the code) that you forgot to dereference the iterator when calling indexOf...

    L 1 Reply Last reply
    3
    • jsulmJ jsulm

      @lansing Then why not use index from https://doc.qt.io/qt-5/qcombobox.html#activated to activate the tab?

      "But when I use it with indexof() I got the error" - well, I guess (you don't show the code) that you forgot to dereference the iterator when calling indexOf...

      L Offline
      L Offline
      lansing
      wrote on last edited by
      #6

      @jsulm said in QTabWidget set current tab by tab name?:

      dereference the iterator

      This is my slot catching the combobox signal, I don't know how to dereference it

          auto pred = [tabName](const FilterWidget &item) {
              return item.tabName== tabName;
          };
      
          QVector<FilterWidget>::iterator it =
                  std::find_if(std::begin(m_filterWidgetVector),
                               std::end(m_filterWidgetVector), pred);
      
          if (it != std::end(m_filterWidgetVector)) {         
              int foundIndex = m_ui->myTabWidget->indexOf(it);
              m_ui->myTabWidget->setCurrentIndex(foundIndex);
      
      
      JonBJ 1 Reply Last reply
      0
      • L lansing

        @jsulm said in QTabWidget set current tab by tab name?:

        dereference the iterator

        This is my slot catching the combobox signal, I don't know how to dereference it

            auto pred = [tabName](const FilterWidget &item) {
                return item.tabName== tabName;
            };
        
            QVector<FilterWidget>::iterator it =
                    std::find_if(std::begin(m_filterWidgetVector),
                                 std::end(m_filterWidgetVector), pred);
        
            if (it != std::end(m_filterWidgetVector)) {         
                int foundIndex = m_ui->myTabWidget->indexOf(it);
                m_ui->myTabWidget->setCurrentIndex(foundIndex);
        
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #7

        @lansing said in QTabWidget set current tab by tab name?:

        int foundIndex = m_ui->myTabWidget->indexOf(it);

        int foundIndex = m_ui->myTabWidget->indexOf(*it);

        On a separate note, I don't mean to confuse you but:

        QVector<FilterWidget>

        Assuming your FilterWidget really is a QWidget, it would be more usual to store pointers to widgets, QVector<FilterWidget *>, but I don't know what the rest of your code for this looks like....

        L 1 Reply Last reply
        0
        • JonBJ JonB

          @lansing said in QTabWidget set current tab by tab name?:

          int foundIndex = m_ui->myTabWidget->indexOf(it);

          int foundIndex = m_ui->myTabWidget->indexOf(*it);

          On a separate note, I don't mean to confuse you but:

          QVector<FilterWidget>

          Assuming your FilterWidget really is a QWidget, it would be more usual to store pointers to widgets, QVector<FilterWidget *>, but I don't know what the rest of your code for this looks like....

          L Offline
          L Offline
          lansing
          wrote on last edited by
          #8

          @JonB

          I got the error

          error: no viable conversion from 'FilterWidget' to 'QWidget *'
          qtabwidget.h:107:26: note: passing argument to parameter 'widget' here
          
          JonBJ 1 Reply Last reply
          0
          • L lansing

            @JonB

            I got the error

            error: no viable conversion from 'FilterWidget' to 'QWidget *'
            qtabwidget.h:107:26: note: passing argument to parameter 'widget' here
            
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #9

            @lansing
            I have already answered that in the second part of my post.
            You really ought know what you're doing with QWidgets versus QWidget *s instead of randomly tyrying things and getting errors, basic C++.

            Without correcting that (which I think you should, but as I said that's a separate matter), I am not sure but you may be able to make it work from where you are now via

            int foundIndex = m_ui->myTabWidget->indexOf(&*it);
            
            L 1 Reply Last reply
            2
            • JonBJ JonB

              @lansing
              I have already answered that in the second part of my post.
              You really ought know what you're doing with QWidgets versus QWidget *s instead of randomly tyrying things and getting errors, basic C++.

              Without correcting that (which I think you should, but as I said that's a separate matter), I am not sure but you may be able to make it work from where you are now via

              int foundIndex = m_ui->myTabWidget->indexOf(&*it);
              
              L Offline
              L Offline
              lansing
              wrote on last edited by lansing
              #10

              @JonB

              The FilterWidget is a struct storing a few widgets and some properties, those widgets has pointers.

              struct FilterWidget {
                  ImageFilter* imageFilter; 
                  TextFilter* textFilter;
                  QString tabName;
              };
              
              QVector<FilterWidget> m_filterWidgetVector;
              

              indexOf(&*it) returns the error

              error: cannot initialize a parameter of type 'QWidget *' with an rvalue of type 'FilterWidget *'
              
              JonBJ 1 Reply Last reply
              0
              • L lansing

                @JonB

                The FilterWidget is a struct storing a few widgets and some properties, those widgets has pointers.

                struct FilterWidget {
                    ImageFilter* imageFilter; 
                    TextFilter* textFilter;
                    QString tabName;
                };
                
                QVector<FilterWidget> m_filterWidgetVector;
                

                indexOf(&*it) returns the error

                error: cannot initialize a parameter of type 'QWidget *' with an rvalue of type 'FilterWidget *'
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #11

                @lansing
                Then I give up. I don't know what you think you are doing. You know that myTabWidget->indexOf() takes a QWidget * as the widget to try to find, and you are passing it some struct (which you have unadvisedly chosen to name FilterWidget [and also how can you possibly make a member named tabName as of type int?]) that has nothing to do with a widget, so how do you expect it to work? Like I wrote, you're not supposed to bung random things in and see what happens, you really need to understand what is going on to write the correct code....

                L 1 Reply Last reply
                0
                • JonBJ JonB

                  @lansing
                  Then I give up. I don't know what you think you are doing. You know that myTabWidget->indexOf() takes a QWidget * as the widget to try to find, and you are passing it some struct (which you have unadvisedly chosen to name FilterWidget [and also how can you possibly make a member named tabName as of type int?]) that has nothing to do with a widget, so how do you expect it to work? Like I wrote, you're not supposed to bung random things in and see what happens, you really need to understand what is going on to write the correct code....

                  L Offline
                  L Offline
                  lansing
                  wrote on last edited by
                  #12

                  @JonB

                  Then tell me what did I do wrong except naming the struct with the "widget" name? The int tabName is a typo.

                  mrjjM jsulmJ 2 Replies Last reply
                  0
                  • L lansing

                    @JonB

                    Then tell me what did I do wrong except naming the struct with the "widget" name? The int tabName is a typo.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @lansing
                    Hi
                    The indexOf can find the widgets you insert to tabWidget with
                    m_ui->myTabWidget->addTab(filterWidget, tabName);

                    so you can find that index again with m_ui->myTabWidget->indexof(filterWidget)

                    struct FilterWidget cannot be used in this context.

                    But i wonder how
                    ImageFilter* imageFilter;
                    TextFilter* textFilter;
                    relates to the filterWidget you use with addTab
                    is it store in imagefilter or textfilter?

                    L 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @lansing
                      Hi
                      The indexOf can find the widgets you insert to tabWidget with
                      m_ui->myTabWidget->addTab(filterWidget, tabName);

                      so you can find that index again with m_ui->myTabWidget->indexof(filterWidget)

                      struct FilterWidget cannot be used in this context.

                      But i wonder how
                      ImageFilter* imageFilter;
                      TextFilter* textFilter;
                      relates to the filterWidget you use with addTab
                      is it store in imagefilter or textfilter?

                      L Offline
                      L Offline
                      lansing
                      wrote on last edited by
                      #14

                      @mrjj

                      Hi, the tabs are added with the ImageFilter class with a tab name

                      mrjjM 1 Reply Last reply
                      0
                      • L lansing

                        @mrjj

                        Hi, the tabs are added with the ImageFilter class with a tab name

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #15

                        @lansing
                        Ok so from a FilterWidget item you can find the
                        tab index by
                        m_ui->myTabWidget->indexof(filterWidgetENTRY.imageFilter )
                        or
                        int foundIndex = m_ui->myTabWidget->indexOf(it.imageFilter ); // maybe need * in front

                        L 1 Reply Last reply
                        0
                        • L lansing

                          @JonB

                          Then tell me what did I do wrong except naming the struct with the "widget" name? The int tabName is a typo.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @lansing
                            Ok so from a FilterWidget item you can find the
                            tab index by
                            m_ui->myTabWidget->indexof(filterWidgetENTRY.imageFilter )
                            or
                            int foundIndex = m_ui->myTabWidget->indexOf(it.imageFilter ); // maybe need * in front

                            L Offline
                            L Offline
                            lansing
                            wrote on last edited by
                            #17

                            @mrjj

                            Thanks the second one works!

                            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