| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 1 | |
Hello, I have a question, may be not a Qt specific one. I have a QList from a derived type and want to cast this QList to a QList of a base type. for example: QList<QLineEdit *> ledList; ledList.append(new QLineEdit()); QList<QWidget*> widList; widList = (QList<QWidget *>) ledList; Have anyone an idea, how I can reach this, without creating a new QList by iterating and cast the objects one by one? May be implementing a cast-operator? Thanks, Marcel
I have no idea, but I am trying to figure out _why_ you want to do something like that. I simply cannot make sense of it. Could you enlighten us? Why not take a QList<QWidget *> to begin with? Guido On Tue, Sep 04, 2007 at 04:01:10PM +0200, Marcel Lehmann wrote: > Hello, > > I have a question, may be not a Qt specific one. > > I have a QList from a derived type and want to cast this QList to a QList of a base type. > for example: > > QList<QLineEdit *> ledList; > ledList.append(new QLineEdit()); > > QList<QWidget*> widList; > widList = (QList<QWidget *>) ledList; > > Have anyone an idea, how I can reach this, without creating a new QList by iterating and cast the objects one by one? > May be implementing a cast-operator? > > Thanks, > Marcel -- [ signature omitted ]
QWidget and QLineEdit only be examples. I ve some basic classes that work with the QList of the base type (eg. QWidget) and do some operations on each element of the QList. Then I have some classes that works with the special type (eg. QLineEdit) and do some other operations If I make a QList of the base type (eg. QWidget), then I have to cast in some of my classes like dynamic_cast<QLineEdit *>(widget) to get the special type. Hope you understand my goal. ----- Original Message ----- From: "Guido Seifert" <wargand@xxxxxx> To: "TrolltechInterest" <qt-interest@xxxxxxxxxxxxx> Sent: Tuesday, September 04, 2007 4:21 PM Subject: Re: cast QList from derived type to base type >I have no idea, but I am trying to figure out _why_ you want to do >something like > that. I simply cannot make sense of it. Could you enlighten us? Why not > take a > QList<QWidget *> to begin with? > > Guido > > > On Tue, Sep 04, 2007 at 04:01:10PM +0200, Marcel Lehmann wrote: >> Hello, >> >> I have a question, may be not a Qt specific one. >> >> I have a QList from a derived type and want to cast this QList to a QList >> of a base type. >> for example: >> >> QList<QLineEdit *> ledList; >> ledList.append(new QLineEdit()); >> >> QList<QWidget*> widList; >> widList = (QList<QWidget *>) ledList; >> >> Have anyone an idea, how I can reach this, without creating a new QList >> by iterating and cast the objects one by one? >> May be implementing a cast-operator? >> >> Thanks, >> Marcel > > -- > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with > "unsubscribe" in the subject or the body. > List archive and information: http://lists.trolltech.com/qt-interest/ > -- [ signature omitted ]
On 9/4/07, Guido Seifert <wargand@xxxxxx> wrote: > > I have no idea, but I am trying to figure out _why_ you want to do > something like > that. I simply cannot make sense of it. Could you enlighten us? Why not > take a > QList<QWidget *> to begin with? I can't answer for the original poster, but I've run into this situation myself. I was constructing a list of the QObjects in an application. Once you have a QObject, it is easy to use QObject::parent() and QObject::children() to get all of the related QObjects. But to get a starting place I used QApplication::topLevelWidgets(), but that returns a QList<QWidget*> so I had to treat it as a special case... it would have been useful for me to be able to cast it to a QList<QObject*>. However, after seeing the one of the other posts I understand why this isn't possible in general. Tom
Hi Marcel, > QList<QLineEdit *> ledList; > ledList.append(new QLineEdit()); > QList<QWidget*> widList; > widList = (QList<QWidget *>) ledList; I have no solution for you, but where is the point of doing this? If you want to hide the details, you should use a "QList<QWidget *> ledList" in first place ... but if you do know the specific type anyways, what's the point of that cast? When accessing the members, of course you can write "QWidget * widget = ledList.at(0)" - no explicit cast needed, no matter if it is a "QList<QLineEdit *>" or "QList<QWidget *>". OT: I am fighting a war against (type)-cast-operator in my own C++-code. I defenitely prefer static_cast<type>() and friends ;-) Regards, Malte -- [ signature omitted ]
On Tuesday 04 September 2007 16:01:10 Marcel Lehmann wrote: > Hello, > > I have a question, may be not a Qt specific one. > > I have a QList from a derived type and want to cast this QList to a QList > of a base type. for example: > > QList<QLineEdit *> ledList; > ledList.append(new QLineEdit()); > > QList<QWidget*> widList; > widList = (QList<QWidget *>) ledList; > > Have anyone an idea, how I can reach this, without creating a new QList by > iterating and cast the objects one by one? May be implementing a > cast-operator? Setting aside Guido's question, you can do this by doing a reinterpret_cast. Using the reinterpret_cast will make sure to any readers of your code that it's an unsafe cast and you've assumed the risks that it brings along. You'll probably get lots of warnings on that with recent compilers... (aliasing). There's no guarantee that it'll work everywhere. With current Qt 4, since they're lists of pointers in both sides, the cast will *probably* work (the same cannot be said for non-pointer types). Just don't mail Trolltech support if it breaks :-) -- [ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.
The explanation on why it's not easily possible: http://www.research.att.com/~bs/bs_faq2.html#conversion Regards Jarek On Tuesday 04 September 2007 16:01, Marcel Lehmann wrote: > Hello, > > I have a question, may be not a Qt specific one. > > I have a QList from a derived type and want to cast this QList to a QList > of a base type. for example: > > QList<QLineEdit *> ledList; > ledList.append(new QLineEdit()); > > QList<QWidget*> widList; > widList = (QList<QWidget *>) ledList; > > Have anyone an idea, how I can reach this, without creating a new QList by > iterating and cast the objects one by one? May be implementing a > cast-operator? > > Thanks, > Marcel -- [ signature omitted ]
I make it with a reinterpret_cast.
To have a type safety, i have done this:
--------------------------------------
template<class T>
class SpecialList: public QList<T> {
public:
SpecialList()
: QList()
{
int testVar = T::is_Not_A_BaseClass;
}
virtual ~SpecialList()
{
}
operator QList<BaseClass > ()
{
return reinterpret_cast< QList<BaseClass> &>(*this);
}
};
-------------------------------------
In my BaseClass I have defined a static int is_Not_A_BaseClass.
Thanks all
Marcel
--
[ signature omitted ]
> > The explanation on why it's not easily possible: > > http://www.research.att.com/~bs/bs_faq2.html#conversion > And another: http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.2 Cheers, -- [ signature omitted ]