[Solved] how to refer a dynamically added table widget inside a tab widget?
-
wrote on 13 Dec 2011, 04:12 last edited by
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.... -
wrote on 13 Dec 2011, 04:25 last edited by
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.
-
wrote on 13 Dec 2011, 06:02 last edited by
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.
-
wrote on 13 Dec 2011, 08:24 last edited by
Thank u Andre and Chris...
I used QTableWidget...
when i tried
@
QTableWidget *Temp;
temp=ui->Tab->Table();@i got compiler error...!
-
wrote on 13 Dec 2011, 09:25 last edited by
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). -
wrote on 13 Dec 2011, 12:16 last edited by
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);
@ -
wrote on 14 Dec 2011, 04:44 last edited by
dynamically adding the table widget inside the dynamically added tab
-
wrote on 14 Dec 2011, 04:44 last edited by
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(); }@
-
wrote on 14 Dec 2011, 05:31 last edited by
hey i got it.....
i did dynamic type conversion....:) -
wrote on 14 Dec 2011, 06:13 last edited by
[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.
-
wrote on 15 Dec 2011, 04:47 last edited by
Thank u Andre...
thanks a lot...i did it..:) -
wrote on 15 Dec 2011, 09:02 last edited by
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/12