Important: Please read the Qt Code of Conduct -

[SOLVED] QTabWidget->widget(index) returns null pointer

  • Hi,
    I'm trying to cast a tab widget, which was successfully added by the app, using its index from QWidget* to QTabWidget*, using the following code:
    QTabWidget * t; t = qobject_cast <QTabWidget*>(this->ui->tabwidget->widget(index)); if (t) this->add_new_tab(t, true); else QMessageBox::critical(0 , 0, QString::fromUtf8("NULL POINTER!"));
    While, I'm sure that the tab was successfully added and that the index matches the targeted tab: I get always the "NULL POINTER!" critical message: which means that the t pointer gets always null.
    I need some help.

  • Lifetime Qt Champion


    Unless you are adding a QTabWidget to ui->tabwidget, that's normal. What widget should you have at that index ?

  • @SGaist What's the correct way to retrieve the added QTabWidget, so?

  • is there anyone who has any input in this thread?

  • Lifetime Qt Champion

    That's the correct way, but are you sure that the widget at index is a QTabWidget ?

  • Moderators

    QTabWidget is the widget that manages tabs. You don't add QTabWidgets to it. You add content to that tab widget and what you get back is the index of newly created tab, not a pointer to anything:

    QWidget* someContent = ... 
    QWidget* someMoreContent = ... 
    int someContentIndex = ui->tabWidget->addTab(someContent, "Some content");
    int someMoreContentIndex = ui->tabWidget->addTab(someMoreContent, "Some more content");

    The above adds content from code. If you added tabs in the designer then you can access their content via widget() method. But don't cast them to QTabWidget because they are not (unless you really added a tab widget to tab widget's tab). They are whatever you added For example if you added a QPushButton then you should cast to that. If you added a QLineEdit then cast to that.

  • @SGaist @Chris-Kawa Yes .. But, the main QTabWidget is supposed to contain only the QTabWidgets added using that function:

    MainWindow::add_new_tab ( QString tabtext )
            QWidget * tw;
            tw = new QWidget;
            this->ui->tabWidget->addTab(tw, tabtext);
            QFormLayout * fl = new QFormLayout;
            this->ui->tabWidget->widget((this->ui->tabWidget->count()) ? 0 : this->ui->tabWidget->count()-2)->setLayout(fl);

    [edit: Corrected coding tags, SGaist]

  • Moderators

    Note that this code (very contrived) adds QWidgets to QTabWidget, not QTabWidgets to QTabWidget. It can be simplified like this:

    void MainWindow::add_new_tab (const QString& tabtext ) //don't copy strings, pass them by const reference
        QWidget * tw = new QWidget;
        tw->setLayout(new QFormLayout);
        ui->tabWidget->addTab(tw, tabtext);

    Then, when you want to get a pointer to that widget, you can do it like this:

    int index = ... 
    QWidget* w = ui->tabWidget->widget(index);
    if(w) {
        //do something with the widget, for example add something to its layout:
        w->layout()->addWidget(new QPushButton("Hello"));

    Btw. Please use ``` tags for code longer than one line. It's hard to read it if you inline it like that.

  • @Chris-Kawa @SGaist I tried that code, to check what is the got widget:

                QWidget * w;
                QTabWidget * t;
                w = this->ui->tabWidget->widget(0);
                t = qobject_cast <QTabWidget*>(w);
                if (t)
                    this->add_new_tab(t, QString::fromUtf8("New Tab"));
                    QMessageBox::critical(0 , 0, w->objectName());

    but always I got blank string in the empty critical message, while tabWidget contains more than a QTabWidget. That's a serious bug in my program.

  • Lifetime Qt Champion

    Did you try with the excellent examples @Chris-Kawa shared ?

    From your input either this->ui->tabWidget->widget(0); returns 0 or if not 0 it returns a QWidget that is not a QTabWidget.

    From your code:

    QWidget * tw;
    tw = new QWidget;
    this->ui->tabWidget->addTab(tw, tab text);

    This code adds a new QWidget inside a new tab of your QTabWidget. It does not create a new QTabWidget. QTabWidget is a "container" widget.

  • @SGaist I tried the code of @Chris-Kawa: the pushbutton was added successfully to the child QTabWidget.

    Ah, right I understand all now after looking to a tabwidget I created already, for testing, within the designer: my mistake was missing the correct widget type of such contained tabs. Now I understand that is QWidget and not QTabWidget. Anyway, is it possible to add new tabs to the child tabs (because I need that in my app) ?

  • Lifetime Qt Champion

    Then start by creating that "sub QTabWidget" with all what is needed and then add it to the ui->tab widget

  • @SGaist I mean how to pass the QWidget as a QTabWidget as a parent for adding new chilld tab for it.

  • Lifetime Qt Champion

    You can't automagically transform a QWidget in a QTabWidget. If you want a QTabWidget in a tab of a QTabWidget, then you have to do something like:

    QTabWidget *childTabWidget = new QTabWidget;
    // setup of childTabWidget
    ui-> tabwidget->addTab(childTabWidget);

  • @SGaist That's it, exactly. That solved the whole problem! I can see the child tabs on my app now. My mistake was following a misleading example. Thanks a lot.

Log in to reply