【问题标题】:How to respond to clicking on list items in a list view in qml, C++ qt, in Blackberry 10 Cascades如何响应在 qml、C++ qt、Blackberry 10 Cascades 中的列表视图中单击列表项
【发布时间】:2012-09-20 19:11:09
【问题描述】:

我试图弄清楚如何能够点击黑莓列表中的一个项目。我正在使用 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);
}

CustomerItem.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_);

【问题讨论】:

    标签: c++ qt listview qml blackberry-10


    【解决方案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();
    }
    

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

    【讨论】:

    • onTriggered 不存在。
    【解决方案2】:

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

      onTriggered: {
                      var selectedItem = dataModel.data(indexPath);
                     // do something with the selected item
                   }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多