Bokambo
Bokambo

Reputation: 4480

How to get the texts of all items from QListWidget in Qt?

How can I get the texts of all the widgets in a QListWidget as a QList<QString>?

I can get the list of widget items like this:

QList<QListWidgetItem *> items =
      ui->listWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);

But that's not exactly what I want, I'd like the list of the widget text() properties.

Upvotes: 6

Views: 18893

Answers (2)

keshav bhatt
keshav bhatt

Reputation: 11

int c = ui->listWidget->count();
for (int i = 0; i < c ; ++i){

QString s = QString::number(i); 
QModelIndex *model_index = new QModelIndex(ui->listWidget->model()->index(i,0) ); //0th column since we have one cloumn in listwidget
QString q= model_index->data(Qt::DisplayRole).toString();
qDebug()<<q;

}

Upvotes: 1

Mat
Mat

Reputation: 206831

There is no built-in function for that, you'll need to do it manually.

QList<QString> texts;
foreach(QListWidgetItem *item, items)
  texts.append(item->text());

Or something like that.

Upvotes: 7

Related Questions