2013-08-07 41 views
7
{ 
... 
    nrow = 10;  
    ncol = 1; 

    /*create QListView */ 
    m_listView = new QListView(this); 
    m_listView->setGeometry(QRect(QPoint(0,100), QSize(100, 150))); 

    QStandardItemModel *model = new QStandardItemModel(nrow, 1, this); 

    //fill model value 
    for(int r=0; r<nrow; r++) 
    { 
     QString sstr = "[ " + QString::number(r) + " ]"; 
     QStandardItem *item = new QStandardItem(QString("Idx ") + sstr); 
     model->setItem(r, 0, item); 
    } 

    //set model 
    m_listView->setModel(model); 
    m_listView->setSelectionMode(QAbstractItemView::ExtendedSelection); 
    connect(m_listView, SIGNAL(pressed(QModelIndex)), this, SLOT(hItem(QModelIndex))); 
} 

void MainWindow::hItem(QModelIndex m) 
{ 
    QItemSelectionModel *selectionModel = m_listView->selectionModel(); 

    m_txt2->setText(QString::number(selectionModel->selectedIndexes().at(0),'d',0));//??? 

    //not sure how to get the items selected: index and string per selection  
} 

答えて

18

これは自分のニーズに合わせてテストしたところ、Qt 5.1で動作します。

私はこのラインでそうC++にはかなり新しいです:

foreach(const QModelIndex &index, list){ 

constと逆参照(&)が必要とされている場合、私は知らない - それはの有無にかかわらず動作します。私は、これまで私が見てきたさまざまな例からこのことをまとめました。

おそらくC++をよく理解している人がコメントできます。

void MainWindow::on_keywordsList_clicked(const QModelIndex &index) 
{ 
    QModelIndexList list =keywordListView->selectionModel()->selectedIndexes(); 

    QStringList slist; 
    foreach(const QModelIndex &index, list){ 
     slist.append(index.data(Qt::DisplayRole).toString()); 
    } 
    qDebug() << slist.join(","); 
} 
+3

const refが優れています。 &を速くすると、元のコンテナの意図しない変更に対してconstが安全になります。 –

関連する問題