3

我试图弄清楚如何能够单击黑莓列表中的项目。我正在使用 QML、C++、QT 和 Blackberry 10 Cascades。我已经实现了列表视图,然后我尝试通过查看这个 Twitter 时间线示例来点击列表中的项目(顺便说一句 - 我无法运行该示例)。

我在做什么是行不通的。当我调用 listView_->setListItemManager(new CustomerListItemManager(customerListContainer_)) 时,它会导致列表视图为空白(在我添加该代码之前,列表视图出现了)。

所以基本上如何获得点击列表中的项目并让它响应工作的能力。

无论如何 - 这是我迄今为止尝试过的相关代码:

Container {
    id: customersListContainer
    objectName: "customersListContainer"

    ListView {
        id: customersList
        objectName: "customersList"

        listItemComponents: [
            ListItemComponent {
        type: "item"

        Container {

        HeaderListItem {
            title: ListItemData.firstName + " " + ListItemData.lastName
        }

            StandardListItem {
            title: ListItemData.officePhone + "\t" + ListItemData.cellPhone
            description: ListItemData.email
        }
        ]                           
    }
}

CustomerListItemManager.cpp:

CustomerListItemManager::CustomerListItemManager() {}

CustomerListItemManager::~CustomerListItemManager() {}

VisualNode *CustomerListItemManager::createItem(ListView *list, const QString &type) 
{
    //the CustomerList::getInstance()->customerListContainer returns the customersListContainer (see the qml code above)
    //
    return new CustomerItem(CustomerList::getInstance()->customerListContainer());
}

void CustomerListItemManager::updateItem(ListView *list, VisualNode *control, const   QString &type, const QVariantList &indexPath, const QVariant &data)
{
    QObject* obj = qvariant_cast<QObject *>(data);
    CustomerData* customer = qobject_cast<CustomerData *>(obj);
}

客户项目.cpp:

CustomerItem::CustomerItem(Container *parent) : CustomControl(parent) {}

CustomerItem::~CustomerItem() {}

void CustomerItem::updateItem(const QString text, QDateTime date) {}

void CustomerItem::select(bool select) {

    // Is this where you handle the response to clicking on an item on the list???
    //
    if (select) qDebug() << "item selected";

else;
}

void CustomerItem::reset(bool selected, bool activated) {
select(selected);
}

void CustomerItem::activate(bool activate) { Q_UNUSED(activate); }

在另一个文件中填充列表:

for (int i = 0; i < customers->length(); ++i) {
    groupDataModel_.insert(customers->at(i)
}
listView_->setDataModel(&groupDataModel_);

//the customerListContainer_ is the customersListContainer (see the qml code above)
// 
listView_->setListItemManager(new ListItemManager(customerListContainer_);
4

2 回答 2

1

我以前遇到过这个问题。基本上,从 ListItemComponent 中,您不能直接使用它们的 id 与外部元素进行交互,例如......

我不确切知道您想做什么,但这里有两个可能对您有所帮助的解决方案:

1)使用单击列表中的元素时发出的信号“onTriggered”。这是一个 QML 示例:

onTriggered: {
    console.log("onTriggered");

    // Retrieve the selected item
    var chosenItem = dataModel.data(indexPath);

    // Bind with C++ using a Q_INVOQUABLE method
    controller.launchItem(chosenItem);
}

2) 在 ListItemComponent 中选择元素的情况下,可以使用中间函数。例如,从您的 ListItemComponent QML 定义中,您可以调用:

// Load additional comments
ListItem.view.launchAdditionalCommentButtonPressedAction();

然后将该函数添加到 QML 文件中的 ListView 中:

function launchAdditionalButtonPressedAction() {
    // Bind with C++ using a Q_INVOQUABLE method
    controller.additionalButtonPressed();
}

我不确定这正是您正在寻找的,但我希望这会有所帮助。

于 2013-03-30T10:27:05.433 回答
0

在 ListView 的声明中,使用 onTriggered 事件如下

  onTriggered: {
                  var selectedItem = dataModel.data(indexPath);
                 // do something with the selected item
               }
于 2012-11-11T12:21:10.700 回答