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. [SOLVED] QTabWidget->widget(index) returns null pointer
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 10.0k Views 3 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      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
      0
      • SGaistS SGaist

        Hi,

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

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

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

        ? 1 Reply Last reply
        0
        • ? A Former User

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

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          is there anyone who has any input in this thread?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            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
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS SGaist

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

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by SGaist
                #7

                @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]

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  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.

                  ? 1 Reply Last reply
                  0
                  • Chris KawaC Chris Kawa

                    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.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    @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"));
                                else
                                    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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      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
                      0
                      • SGaistS SGaist

                        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.

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by A Former User
                        #11

                        @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) ?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          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
                          0
                          • SGaistS SGaist

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

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              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
                              0
                              • SGaistS SGaist

                                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);
                                
                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

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

                                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