PDA

View Full Version : typecast of QWidget



wagmare
11th August 2009, 12:46
hi friends,

i got a problem in QList
as i subject all my object in a Widget class to a QList by

QList wlist;
wlist = widget->findChildren<QWidget *>();

this list have QLabel, QTextEdit, etc ..
but all were typecast to QWidget in the QList ..

now i want to access a label in wlist.at(2) .. which currently returning a QWidget pointer ..

please help ..

numbat
11th August 2009, 12:56
Have you tried qobject_cast?

wagmare
11th August 2009, 12:58
thanks for reply ..
yes but i dont know right way to access to on QList.at(i) ...

how to call a typical QLabel function ex:setText on the QList item with type cast ..

spirit
11th August 2009, 13:08
try this


...
QList<QWidget *> wlist = widget->findChildren<QWidget *>();
QLabel *label = qobject_cast<QLabel *>(wlist.at(0));
if (!label)
return;
label->setText("some text");
...

wagmare
11th August 2009, 13:10
yeah it worked .. thanks to all ..

spirit
11th August 2009, 13:12
or you can made even simpler


QList<QLabel *> labels = widget->findChildren<QLabel *>();
if (!labels.count())
return;
labels.at(0)->setText("some text");
...