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 change widget of existing tab
Forum Updated to NodeBB v4.3 + New Features

Qtabwidget change widget of existing tab

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 4.9k Views
  • 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.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by
    #1

    Hello again,
    Is there a function to change the widget of an existing tab in qtabwidget ? In designer, dropping a Tab Widget in the window creates by default 2 tabs. I can drag and drop widgets in each tab, but I want to do it dynamically, from code. I'm looking for something like tabWidget->setWidgetAt(index)=...

    I know I can achieve this by adding new tabs, but I'd like to know if it's possible to edit widgets of the tabs created in the designer.

    J.HilkJ 1 Reply Last reply
    0
    • cpperC cpper

      Hello again,
      Is there a function to change the widget of an existing tab in qtabwidget ? In designer, dropping a Tab Widget in the window creates by default 2 tabs. I can drag and drop widgets in each tab, but I want to do it dynamically, from code. I'm looking for something like tabWidget->setWidgetAt(index)=...

      I know I can achieve this by adding new tabs, but I'd like to know if it's possible to edit widgets of the tabs created in the designer.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @cpper Wll, of course,

      Link to the doku

      with

      QWidget *QTabWidget::widget(int index)
      

      You get a pointer the the widget, with that you can do what ever you want.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

        Thanks for the quick reply.
        I already tried that, but strangely got a lvalue error, so I though that's now the right way to do it:

        ui->tabWidget->widget(0)=new QPushButton();
        
        

        error: C2106: '=': left operand must be l-value

        1 Reply Last reply
        0
        • cpperC Offline
          cpperC Offline
          cpper
          wrote on last edited by cpper
          #4

          Actually the error makes sense, the return type must have been reference to pointer in order for the code to work.
          This makes more sense, but it also doesn't work, because the = operator of QWidget is private.

          *ui->tabWidget->widget(0)=QPushButton();
          
          J.HilkJ 1 Reply Last reply
          0
          • cpperC cpper

            Actually the error makes sense, the return type must have been reference to pointer in order for the code to work.
            This makes more sense, but it also doesn't work, because the = operator of QWidget is private.

            *ui->tabWidget->widget(0)=QPushButton();
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @cpper Ok sry, I know get what you try to do.

            You want to replace a QWidget of your QTabwidget with another one. I'm afraid there is no way around an a QTabWidget::insertTab and QTabWidget::removeTab combination

            but you could easily subclass QTabWidget and make such a function.

            //Untested
            public slots:
               void replaceTab(int index, QWidget *page, QString title){
               if(title.isEmpty()) title = tabText(index);
               auto toDelete = widget(index);
               removeTab(index);
               toDelete->deleteLater();
              insertTab(index, page, title);
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2

            • Login

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