开发者

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

开发者 https://www.devze.com 2023-03-11 05:50 出处:网络
How can I get the texts of all the widgets in a QListWidget as a QList<QString>? I can get the list of widget itemslike this:

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->l开发者_如何学PythonistWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);

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


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.


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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消