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] how to refer a dynamically added table widget inside a tab widget?
Forum Update on Monday, May 27th 2025

[Solved] how to refer a dynamically added table widget inside a tab widget?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 7.2k 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.
  • A Offline
    A Offline
    aurora
    wrote on 13 Dec 2011, 04:12 last edited by
    #1

    Hi,
    I created a tab widget using designer,
    Then added table widget inside the tab widget...(one table in every tab)
    Now i want to access the values of a particular table, for example that table headers....
    How can i get that????

    And if there is multiple tabs,i want headers of presently activated tabs, tables header...
    Please guide me....

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris H
      wrote on 13 Dec 2011, 04:25 last edited by
      #2

      Are you familiar with the Model/View design pattern that Qt uses to handle the data and views of the data in things like tables? If not, you might want to check out the overview "here":http://doc.qt.nokia.com/latest/model-view-programming.html first.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 13 Dec 2011, 06:02 last edited by
        #3

        In general, you would refer to such widgets using pointer variables that you have kept around. They can be directly declared somewhere, or they can be items in some container class, but you have to keep them. If you used a QTableWidget, then this is the way to go. If you used a QTableView, you could refer to the model and the selectionmodel instead.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aurora
          wrote on 13 Dec 2011, 08:24 last edited by
          #4

          Thank u Andre and Chris...
          I used QTableWidget...
          when i tried
          @
          QTableWidget *Temp;
          temp=ui->Tab->Table();@

          i got compiler error...!

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on 13 Dec 2011, 09:25 last edited by
            #5

            Hard to say without having a more code detail and the compile error.
            You have to extract the current tab index from your qtabwidget, then to access the table in it or get its child (if the table is the only one).

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 13 Dec 2011, 12:16 last edited by
              #6

              What makes you think that the ui object has a member Tab that can be dereferenced like you are, and that then has a method Table?

              No matter how items are nested in a .ui file, pointers to all of them end up directly in the ui object. So, if you you have a ui file with a tab, and on that tab there is a QTableWidget with the name Table, you can still access that table using
              @
              ui->Table->someCoolMethod();
              @
              Also, if you have the table widget in a ui file, then you are not really "dynamically adding" it, are you? That would be the case you did something like
              @
              QTableWidget* theTable = new QTableWidget(ui->tab);
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aurora
                wrote on 14 Dec 2011, 04:44 last edited by
                #7

                dynamically adding the table widget inside the dynamically added tab

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  aurora
                  wrote on 14 Dec 2011, 04:44 last edited by
                  #8

                  i'm creating tab widget using designer...and then adding table widget inside the tab widget as shown below...
                  Now how can i get access to table widget?
                  @ void MainWindow::createFilesTable(int TagCount,QString FileName)
                  {

                  filesTable = new QTableWidget(0,TagCount);
                  filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
                  filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
                  filesTable->verticalHeader()->hide();
                  ui->tabWidget->addTab(filesTable,FileName);
                   
                  }@
                  

                  i tried like below to access the table i inserted inside the tab widget,,,,but compiler giving error..!
                  @ void MainWindow::on_pushButton_FilterCollumn_clicked()
                  {

                  QTableWidget *temp=ui->tabwidget->currentWidget();
                   
                  }@
                  
                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aurora
                    wrote on 14 Dec 2011, 05:31 last edited by
                    #9

                    hey i got it.....
                    i did dynamic type conversion....:)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on 14 Dec 2011, 06:13 last edited by
                      #10

                      [quote author="aurora" date="1323837888"]i'm creating tab widget using designer...and then adding table widget inside the tab widget as shown below...
                      Now how can i get access to table widget?
                      @ void MainWindow::createFilesTable(int TagCount,QString FileName)
                      {

                      filesTable = new QTableWidget(0,TagCount);
                      filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
                      filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
                      filesTable->verticalHeader()->hide();
                      ui->tabWidget->addTab(filesTable,FileName);
                       
                      }@
                      

                      [/quote]

                      Well, how about you just keep a pointer to the table around somewhere? Perhaps something along the lines of:
                      @
                      //in header of class
                      QHash<QString, QTableWidget*> m_fileTables;

                      //in your .cpp
                      void MainWindow::createFilesTable(int TagCount,QString FileName)
                      {
                      filesTable = new QTableWidget(0,TagCount);
                      filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
                      filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
                      filesTable->verticalHeader()->hide();
                      ui->tabWidget->addTab(filesTable,FileName);
                      m_fileTables->insert(FileName, filesTable); // <--- Keep a pointer to the widget, accessible via the associated FileName
                      }
                      @

                      I now put the pointer in a hash with the FileName as key, but you can also choose some other convenient key of course.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aurora
                        wrote on 15 Dec 2011, 04:47 last edited by
                        #11

                        Thank u Andre...
                        thanks a lot...i did it..:)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on 15 Dec 2011, 09:02 last edited by
                          #12

                          I am marking the topic as [Solved] then. Next time, you can do that yourself by using the Edit link next to the first posting in this topic.

                          1 Reply Last reply
                          0

                          1/12

                          13 Dec 2011, 04:12

                          • Login

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