试图在QListView中显示列表?

时间:2013-08-07 17:04:38

标签: c++ qt qlistview

无法显示列表?

enter image description here

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

modelA = 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);
    modelA->setItem(r, 0, item);
}
//set model
m_listViewA->setModel(modelA);
m_listViewA->setSelectionMode( QAbstractItemView::ExtendedSelection );

QStringList slist;
foreach(const QStandardItem index, modelA) //ERROR
{
    slist.append( index.data(Qt::DisplayRole ).toString());
}

1 个答案:

答案 0 :(得分:0)

这有效......但宁可使用“foreach”......

QString strA;
for(int r=0; r < modelA->rowCount(); r++)
{
    strA += "\r\n" + modelA->item(r,0)->text();
}
QMessageBox *msgBox = new QMessageBox(0);
msgBox->setGeometry(QRect(QPoint(200,200),QSize(400,400)));
msgBox->setInformativeText(strA);
msgBox->exec();