簡體   English   中英

如何獲取在QListView中選擇的行的索引或字符串?

[英]How to get the index or string of the row selected in the QListView?

不確定如何顯示列表中單擊的項目或索引?

{
...
    nrow = 10;     
    ncol = 1;

    /*create QListView */
    m_listView = new QListView(this);
    m_listView->setGeometry(QRect(QPoint(0,100), QSize(100, 150)));
    connect(m_listView, SIGNAL(clicked(QModelIndex)), this, SLOT(hItem()));

    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);
    }
}

void MainWindow::hItem()
{
    m_txt1->setText(item@clicked);
}

QListView發出的信號具有一個QModelIndex對象作為參數。 編輯與信號簽名匹配的插槽: void MainWindow::hItem(QModelIndex idx)

使用QModelIndex對象,您可以使用column()row()來檢索單擊的行。 參見此處: http : //harmattan-dev.nokia.com/docs/library/html/qt4/qmodelindex.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM