Qtabwidget change widget of existing tab



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



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



  • 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



  • 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();
    


  • @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);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.