2012-09-28 22 views
3

我想弄清楚如何能夠點擊黑莓列表中的項目。我使用的是QML,C++,QT和Blackberry 10 Cascades。我已經實現了列表視圖,然後我嘗試着讓它可以通過查看這個Twitter時間軸示例來點擊列表上的一個項目(順便說一句 - 我無法運行該示例)。如何響應點擊qml列表中的列表項,在c + + qt中,在黑莓10瀑布

我在做什麼是行不通的。當我調用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_); 

回答

0

在ListView控件的聲明中使用onTriggered事件爲以下

onTriggered: { 
        var selectedItem = dataModel.data(indexPath); 
       // do something with the selected item 
       } 
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(); 

,然後添加功能,以您的ListView在你的QML文件:

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

我不知道這到底是什麼你正在尋找,但我希望這有助於。

+1

onTriggered不存在。 – Michael

相關問題