0

So I have FINALLY gotten to the point where I can select multiple items on a ListView:

       ListView {
                id: lv_stuffs
                horizontalAlignment: HorizontalAlignment.Fill

                dataModel: _app.personDataModel //REFERENCE 1

                multiSelectAction: MultiSelectActionItem {
                }

                multiSelectHandler {
                    actions: [
                        // Add the actions that should appear on the context menu
                        // when multiple selection mode is enabled
                        ActionItem {
                            title: "Search for stuffs"                            
                            onTriggered: {                                    
                                _app.search(lv_stuffs.selectionList());
                            }       
       ...     

And I am sending this selection list through to my search method:

void ApplicationUI::search(const QVariantList &list)
{
    alert(QString("%1 items selected").arg(list.length()));
    alert(((Person)list.at(0)).firstName);//<---- THIS IS THE PROBLEM
}      

I am trying to get the "Person" object out of the GroupedDataModel that originally bound to the item... and I have to say I am more than a little stumped. The person is being added to the personDataModel via a simple insert method in a database class:

personDataModel->insert(person);

and the items are then bound to the ListView in the QML (REFERENCE 1 above). The binding is all fine and the items are visible in the list. What I can't figure out is how to now extract these "Person" objects out of the QVariantList I am sent via the MultiSelectionMethod.

My person class:

Person::Person(QObject *parent) : QObject(parent){}

Person::Person(const QString &id, const QString &firstname, const QString &lastname, QObject *parent)
    : QObject(parent)
    , m_id(id)
    , m_firstName(firstname)
    , m_lastName(lastname)
{
}

QString Person::customerID() const
{
    return m_id;
}

QString Person::firstName() const
{
    return m_firstName;
}

QString Person::lastName() const
{
    return m_lastName;
}

void Person::setCustomerID(const QString &newId)
{
    if (newId != m_id) {
        m_id = newId;
        emit customerIDChanged(newId);
    }
}

void Person::setFirstName(const QString &newName)
{
    if (newName != m_firstName) {
        m_firstName = newName;
        emit firstNameChanged(newName);
    }
}

void Person::setLastName(const QString &newName)
{
    if (newName != m_lastName) {
        m_lastName = newName;
        emit lastNameChanged(newName);
    }
}

I have been PAINFULLY following this tutorial here, https://developer.blackberry.com/cascades/documentation/ui/lists/list_view_selection.html, which conveniently stops right where my question begins.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95

2 Answers2

0

Are you perhaps looking for the value function?

void ApplicationUI::search(const QVariantList &list)
{
    alert(QString("%1 items selected").arg(list.length()));
    alert(((Person)list.value(0)).firstName);
}   

(syntax of the extraction of the firstName value may not be right there, depends on your implementation)

TheSmurf
  • 15,337
  • 3
  • 40
  • 48
  • Thanks, but this does not work - I have gotten so far as: QVariant nlist= personDataModel->data(list.at(0).toList()); qDebug() << "List: " << nlist; Results in List: QVariant(QObject*, Person(0x857c310)) But I can't get the items out of the QVariant – Quintin Balsdon Jun 19 '13 at 06:07
  • Use the value() function on the QVariant you've got. That should give you the contained person. – TheSmurf Jun 20 '13 at 18:57
  • That SOUNDS right, but value is a template - so I have to say something like QVariant tlist = personDataModel->data(list.at(0).toList()); Person* selectedPeople = tlist.value(); And I get the error: "'qt_metatype_id' is not a member of 'QMetaTypeId'" Now before you just Google that error and give me the results - I have no idea where to put those lines of code in the class def (maybe not even in the class def) - maybe not even those lines of code. Why the hell is this so complex? – Quintin Balsdon Jun 20 '13 at 21:36
0

Your Person class will be stored in a QVariant. To accomplish this, Qt has to generate some code specific to your class. It can be done by adding this right after your class definition, in the header file for example: Q_DECLARE_METATYPE(Person). You can read more about this here: http://qt-project.org/doc/qt-4.8/qmetatype.html#Q_DECLARE_METATYPE

Now, to extract the value as a Person object, you can use QVariant::value<T>() (http://qt-project.org/doc/qt-4.8/qvariant.html#value): alert(list.at(0).value<Person>().firstName());

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75