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



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



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



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



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

    i got compiler error...!



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



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



  • dynamically adding the table widget inside the dynamically added tab



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


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



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



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



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


Log in to reply
 

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