lpostula
lpostula

Reputation: 584

Insert Items dynamically in a QListWidget

What I would like to do is taking input from a sql database and put all these datas in a QListWidget, but I don't know how many of them there gonna be, I need also to know the id of which one was clicked when clicked.

Any ideas?

Upvotes: 0

Views: 1419

Answers (1)

Julian
Julian

Reputation: 1542

if str is the label from your sql query and n is the id then:

create your items with:

QListWidgetItem* i = new QListWidgetItem(str);

Set the id with:

i->setData(Qt::UserRole, n);

and add it to the widget:

myListWidget->addItem(i);

Then when its clicked you will get the signal

void QListWidget::itemActivated ( QListWidgetItem * item ) [signal]

connect this to a slot in your class and get the id back with

item->data(Qt::UserRole).toInt();

But this is also a good time to use QTableView and QSqlQueryModel.

Upvotes: 1

Related Questions