PDA

View Full Version : parsing QtableWidget from a QStackedWidget



rosmarcha
12th October 2006, 15:14
Hi,

I have a QStackedwidget that contains multiple QTableWidget. How can i parse all of the
QTablewidget from the Qstackedwidget.

example of my test, and the dynamic_cast is always false , is there a better way ?

for (int i=0;i<stackedWidget->count() ; i++)
{
QWidget *poChild = stackedWidget->widget(i);
if (poChild && dynamic_cast<QTableWidget*>(poChild) )
{
QTableWidget *poTBL = dynamic_cast<QTableWidget*>(poChild);
poTBL->setColumnCount(0);
poTBL->setRowCount(0);
}
}

thanks,

munna
12th October 2006, 15:21
You can use setObjectName() and objectName() to identify your tableWidgets.

rosmarcha
12th October 2006, 15:35
i want to make abstaction of the name, assume i don't know the name, there must be a way to iterate true children and cast them in the current type ?

Thanks

rosmarcha
12th October 2006, 16:11
maybee it's me that does not understand their architecture correclty :) . so if the method
widget() returns me a qwidget, can i retreive the content of the widget, which i know are
qTableWidget.


for (int i=0;i<stackedWidget->count() ; i++)
{
QWidget *poChild = stackedWidget->widget(i); // this works

}

jpn
12th October 2006, 16:12
How do you add the widgets to the stacked widget?

rosmarcha
12th October 2006, 16:35
i think it's because of the way it's displayed in the gui designer, If i look at the generated code

stackedWidget = new QStackedWidget(centralwidget);
stackedWidget->addWidget(page_0);
page_1 = new QWidget();
gridLayout2 = new QGridLayout(page_1);
imageTBL = new QTableWidget(page_1);

gridLayout2->addWidget(imageTBL, 0, 0, 1, 1);
stackedWidget->addWidget(page_1);

from that code how can i retreive the Qtable widget from the stackedWidget

thanks,

jpn
12th October 2006, 16:43
Ok so the problem is that the widget being in the stack is not a QTableWidget. It's just a QWidget (page) which has a layout containing the table widget. But if you don't put any other widget into the layout, there is no point creating an additional wrapper widget and a layout.



stackedWidget = new QStackedWidget(centralwidget);
stackedWidget->addWidget(page_0);
//page_1 = new QWidget();
//gridLayout2 = new QGridLayout(page_1);
imageTBL = new QTableWidget(stackedWidget);

//gridLayout2->addWidget(imageTBL, 0, 0, 1, 1);
stackedWidget->addWidget(imageTBL);


EDIT: Errr.. ignore this. I don't know what did I have on my mind. Of course the table widget needs to be in a layout so that it's geometry gets managed at all.

rosmarcha
12th October 2006, 16:55
the problem here is that this header code is generated by the QT code generator uic.bat and we need to keep it as is.

i guess that there are no easy way to retreive the object in a loop

jpn
12th October 2006, 17:14
Maybe it would be more flexible choice to use a QStackedLayout from code. You could insert the table widget directly to the stacked layout without using an additional wrapper widget for layouting like designer does..

jrideout
13th October 2006, 13:54
have you tried using a qobject_cast?

jpn
13th October 2006, 14:36
have you tried using a qobject_cast?
This won't work because the widget returned by the stacked widget is not a QTableWidget. It's just a plain QWidget which has a layout containing the QTableWidget. QObject has methods for looking up children of specific type etc. These methods can be used to find the QTableWidget child.