PDA

View Full Version : Changing the type of QList



wagmare
19th October 2011, 06:53
Hi friends,

I was going though the qobject_cast of a QLineEdit to QWidget ... now i got the problem that im having a QList of QLineEdit and have to type cast it to QList of QWidget ..
present i was typecasting each and every QlineEdit in the list in a for() loop..

but is there any other way to change the type of a QList
Ex:

QList <QLineEdit *> lList can be typecasted to QList<QWidget*> as QLineEdit is inherited from QWidget ..


Thanks

Wagmare

FelixB
19th October 2011, 07:37
why do you want to do that?

wagmare
19th October 2011, 09:50
Thanks for reply Felix

Im having a common function having the argument
QList<QWidget *> say void myWidget::enableAll(QList<QWidget *> list)
and
im getting all the QLineEdit in one widget in a QList<QLineEdit *> lList using findChild and i have to pass to the function say
enableAll(lList)..

in this case i have to typecast each and every QLineEdit into QWidget so i can pass it as a argument ..

ex:

for(int i =0; i < lList.size(); i++)
{
lList[i] = qobject_cast<QWidget*>(lList[i]);
}

enableAll(&lList);


but is there any other way i can easily cast a QList<QLineEdit> to QList<QWidget *>

Thanks & Regards

stampede
19th October 2011, 11:51
"enableAll()" could be a template method:


class MyClass ... {
protected:
template< typename T >
void enableAll( QList<T*>& widgets ){
foreach( T * t, widgets ){
t->setEnabled(true);
}
}
};

You can use it like any other method now:


QList<QWidget*> widgets;
QList<QLineEdit*> lineEdits;

enableAll(widgets);
enableAll(lineEdits);


If you use it with a type that does not have "setEnabled()" method, you will get a compile time error.

----
edit:
I think VC++ does not support member template methods, I'm using gcc, its supported there. If you are using a compiler which does not support this feature, just make this method a regular template (not a member of a class).

----
@down:
ok, my mistake :)

FelixB
19th October 2011, 11:59
I think VC++ does not support member template methods

VS2008 does :)