PDA

View Full Version : how to refer a dynamically added table widget inside a tab widget?



aurora
13th December 2011, 05:13
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....

aurora
13th December 2011, 09:20
Please somebody look at this issue...!!

^NyAw^
13th December 2011, 10:57
Hi,

You can use a QList of QTableWidget pointers. The index of the current Tab will point to the QTableWidget.
In the constructor of your application you can fillup the QList.

Spitfire
13th December 2011, 10:57
You should have pointer of you QTabWidget.
Connect currentChanged( index ) signal from that widget to a slot that will do what you want when tab is changed.
For example use QTabWidget::widget( index ) to get a current page widget and - if you don't have pointer to you table cached - iterate through its children looking for your table.

Simpler may be to create a cache of table pointers indexed the same as pages they are on.

Look at Qt Documentation for QTabWidget (http://doc.qt.nokia.com/latest/qtabwidget.html)and all you need to know is there.

aurora
14th December 2011, 06:31
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();

}


its telling it cant convert 'QWidget' to 'QTableWidget'!!!
Please tell me what changes i need to do?
And also how can i get that table's header?

Added after 50 minutes:

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

^NyAw^
14th December 2011, 12:51
Hi,

As I told you, you can use a QList of QTableWidget pointers like this:







void MainWindow::createFilesTable(int TagCount,QString (http://doc.qt.nokia.com/latest/qstring.html) FileName)

{



filesTable = new QTableWidget (http://doc.qt.nokia.com/latest/qtablewidget.html)(0,TagCount);

filesTable->setSelectionBehavior(QAbstractItemView (http://doc.qt.nokia.com/latest/qabstractitemview.html)::SelectRows);

filesTable->horizontalHeader()->setResizeMode(0, QHeaderView (http://doc.qt.nokia.com/latest/qheaderview.html)::Stretch);

filesTable->verticalHeader()->hide();

ui->tabWidget->addTab(filesTable,FileName);

tableList.append(filesTable); //Add the QTableWidget to the list

}




Then you can, as Spitfire told you, connect currentChanged( index ) signal to a slot and use index variable to obtain the QTableWidget pointer from the list:



void MainWindow::tabIndexChanged(int index)
{
QTableWidget* tableWidget = tableList.at(index);
...
}


Of course that you can use your method, but what happens if there is more than one widget into the tab? You have to search on the child widgets if there is one QTableWidget. but there will be another problem if there is more that one QTableWidget childs into the tab.