简体   繁体   中英

How to cast QList<QObject *> to QList<ADerivedQObjectClass*>

I can't find a way to cast a QList of QObjects to another QList with another class template. I have a function that creates instances of QObjects and then return a QList . So my question is, can I cast that QList to another QList of different template class pointers ?

My code:

QList<QObject *> DbManager::listVO(QString voname)
{
    DbManager::VOPropertiesJoin(DbManager::VOKeys(obj),VOJOIN_BOTH,"=");
    QList<QObject *> vos;

    QString voquery = "select * from %1";
    voquery = voquery.arg(voname);
    QSqlQueryModel *volist = DbManager::makeSelect(voquery);

    for(int i = 0;i < volist->rowCount();i++){
        QSqlRecord record =volist->record(i);
        QObject *voinstance = DbManager::instantiateVO(voname,record.value(0),record.value(1),record.value(2),record.value(3),record.value(4),record.value(5),record.value(6),record.value(7));
        vos << voinstance;
    }
    return vos;
}

and I want something like this:

QList<AnyClass*> list = DbManager::listVO("AnyClass");

Thanks for help in advance.

Since what you need at the end is a QList<AnyClass*> , you can try something like this (Assuming AnyClass is derived from QObject )

Note: I did some modifications to your original code to pass your list by reference since returning a huge list might not that be efficient. I didn't compile the code, but hoping you can figure out what is happening inside this snippet

void DbManager::listVO(QString voname, QList<AnyClass*>& listAnyClass)
{
    DbManager::VOPropertiesJoin(DbManager::VOKeys(obj),VOJOIN_BOTH,"=");

    // Initialise list with an empty list
    listAnyClass = QList<AnyClass*>();

    QString voquery = "select * from %1";
    voquery = voquery.arg(voname);
    QSqlQueryModel *volist = DbManager::makeSelect(voquery);

    for(int i = 0; i < volist->rowCount(); i++)
    {
        QObject *voinstance = GetInstance(voname, volist->record(i));

        // Trying to cast into an AnyClass
        AnyClass* pAnyClass = qobject_cast<AnyClass*>(voinstance);

        // If pAnyClass is a valid AnyClass* update your list
        if(pAnyClass)
        {
            listAnyClass.append(pAnyClass);
        }
    }
}

QObject* DbManager::GetInstance(QString sVoname, const QSqlRecord& record)
{
    return DbManager::instantiateVO
    (
        voname,
        record.value(0),
        record.value(1),
        record.value(2),
        record.value(3),
        record.value(4),
        record.value(5),
        record.value(6),
        record.value(7)
    )
}

And after calling DbManager::listVO(QString voname, QList<AnyClass*>& listAnyClass) function, listAnyClass will get populated accordingly. This is efficient with contrast to returning a list.

Edited:

So in a nutshell you want to convert a QList<QObject*> into QList<AnyClass*> . So keep your original code as it is and implement a method like this where you wanna do this conversion:

void ConvertToAnyClassList(const QList<QObject*>& listQObjects, QList<AnyClass*>& listAnyClass)
{
    listAnyClass = QList<AnyClass*>();
    for( int i = 0; i < listQObjects.length(); ++i )
    {
        AnyClass* pAnyClass = qobject_cast<AnyClass*>(listQObjects.at(i));

        if(pAnyClass)
        {
            listAnyClass.append(pAnyClass)
        }
    }
}

And you can call it like this:

QList<QObject*> listQObjects = DbManager::listVO("AnyClass");
QList<AnyClass*> listAnyClass;
ConvertToAnyClassList(listQObjects,listAnyClass);

Suppose you have multiple types, so you may want to implement functions for each type like this:

ConvertToUserList(listQObjects,listUserObjects);
ConvertToCountryList(listQObjects,listCountryObjects);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM