QListWidget same name's with different id's from sqlite query should be shown in qlineedit by clicking the item
-
Hi,
I need your help. I'm new to Qt. I want to implement the following, but I don't know how.
I have a QListWidget with items like this:
Name A
Name A
Name B
Name A
Name A
Name A
...In the backend there is an sqlite database with an N:M - Relationship. If I click on the first item (Name A) the sql query will return a QList<int> with all ids where name = Name A. In my example the QList<int> ids has 5 id's.
Now I want to reach that the returned id is shown in a QLineEdit. For Example:
First "Name A" shows id 1
Second "Name A" shows id 2
Name B shows another id for example 2000
Third "Name A" shows id 3
and so on..How can I do this?
Second question: How can I handle this, that the first Name A gets always id 1, second name always id 2 ans so on?
Here is my example code thats works only for one id. For example Name B.
void MainWindow::on_fungusList_itemSelectionChanged() { const QString name = ui->fungusList->currentItem()->text(); const QList<int> ids = mDatabaseManager->gnameId(name); if(ids.isEmpty()) { return; } else if (ids.count() == 1) { const QString idString = QString::number(ids.first()); ui->number->setText(idString); return; } }And this is my database query
QList<int> DatabaseManager::gnameId(const QString &name) const { QList<int> ids; QSqlQuery query; query.prepare("SELECT DPilzname.PilzID FROM DName " "JOIN DPilzname ON DPilzname.DNameID = DName.ID " "JOIN Pilze ON DPilzname.PilzID = Pilze.ID " "WHERE DName.Vollname = :name"); query.bindValue(":name", name); query.exec(); while (query.next()) { ids.append(query.value(0).toInt()); } return ids; }I hope some know's how to implement this.
Thanks
-
Hi
You can store extra info in the QListWidgetItem, so you can assign an id when you create them.const int IDRole = Qt::UserRole + 1; QListWidgetItem *item = new QListWidgetItem("test"); item->setData(IDRole, 2000 ); // later, in the on_fungusList_itemSelectionChanged int Id = item->data(IDRole).toInt();Im not sure how your ID counting works so yo might need something extra than simply counting the A and increase the ID.
Not sure how ID 2000 for Name B works. -
Hi,
I need your help. I'm new to Qt. I want to implement the following, but I don't know how.
I have a QListWidget with items like this:
Name A
Name A
Name B
Name A
Name A
Name A
...In the backend there is an sqlite database with an N:M - Relationship. If I click on the first item (Name A) the sql query will return a QList<int> with all ids where name = Name A. In my example the QList<int> ids has 5 id's.
Now I want to reach that the returned id is shown in a QLineEdit. For Example:
First "Name A" shows id 1
Second "Name A" shows id 2
Name B shows another id for example 2000
Third "Name A" shows id 3
and so on..How can I do this?
Second question: How can I handle this, that the first Name A gets always id 1, second name always id 2 ans so on?
Here is my example code thats works only for one id. For example Name B.
void MainWindow::on_fungusList_itemSelectionChanged() { const QString name = ui->fungusList->currentItem()->text(); const QList<int> ids = mDatabaseManager->gnameId(name); if(ids.isEmpty()) { return; } else if (ids.count() == 1) { const QString idString = QString::number(ids.first()); ui->number->setText(idString); return; } }And this is my database query
QList<int> DatabaseManager::gnameId(const QString &name) const { QList<int> ids; QSqlQuery query; query.prepare("SELECT DPilzname.PilzID FROM DName " "JOIN DPilzname ON DPilzname.DNameID = DName.ID " "JOIN Pilze ON DPilzname.PilzID = Pilze.ID " "WHERE DName.Vollname = :name"); query.bindValue(":name", name); query.exec(); while (query.next()) { ids.append(query.value(0).toInt()); } return ids; }I hope some know's how to implement this.
Thanks
@Gabber
In addition to @mrjj. Since you seem to have some ID associated with each name, consider using the following:-
Make your SQL query return whatever is appropriate for two columns for each entry: the name plus that name's associated ID.
-
Store that in a (2 column) model.
-
Change to a
QListView, which is whatQListWidgetis derived from. Use the model for theQListView, and pick the name column for what it should display. -
Maybe change to a
QTableView, which shows multiple columns against each row, unlike aQListView. Show the user the name plus the ID column. With just one column/list view it is not clear who your end-user will know to distinguish between two identical names which have different IDs.
-
-
I think you have misunderstood me. I have made a screenshot . If I click on the item "Dünnsporiges Kranzbecherchen" in QListWidget, it will search in my database after "Dünnsporiges Kranzbecherchen".
After that query I get a QList<int> with all id's that where found in the query (in my case I got 5 id's).
If I click on the first "Dünnsporiges Kranzbecherchen" it should select the first id from the query and show the number in the QLineEdit "Nummer" on the right sight of the screenshot. Let's say this is id 1. If I click on the second "Dünnsporiges Kranzbecherchen" it should display the next id for example id 2. If I click on the third "Dünnsporiges Kranzbecherchen" it should display the third id from the database query and so on.Now when I go back to click the first "Dünnsporiges Kranzbecherchen" it should select id 1 again.
How can I make this as simple as possible?
Thanks for all your help :)
-
Hi
You can store extra info in the QListWidgetItem, so you can assign an id when you create them.const int IDRole = Qt::UserRole + 1; QListWidgetItem *item = new QListWidgetItem("test"); item->setData(IDRole, 2000 ); // later, in the on_fungusList_itemSelectionChanged int Id = item->data(IDRole).toInt();Im not sure how your ID counting works so yo might need something extra than simply counting the A and increase the ID.
Not sure how ID 2000 for Name B works.@mrjj said in QListWidget same name's with different id's from sqlite query should be shown in qlineedit by clicking the item:
Qt::UserRole
After quite a while and a lot of reading on the Internet, I understood your approach. It also works perfectly. Thank you very much.
But I still have one question. Can you explain the Qt::UserRole + 1 in more detail. The documentation I found is not very informative for me. In particular I would be interested in what +1 does?
Thanks :)
-
@Gabber said in QListWidget same name's with different id's from sqlite query should be shown in qlineedit by clicking the item:
in what +1 does?
It adds 1 to the enum value of Qt::UserRole.
-
Hi
It's nothing fancy at all.The UserRole is a value from where user-defined roles start.
So the Qt roles have lesser values then this
The +1 syntax is just a habit I have for setting it upconst int IDRole = Qt::UserRole + 1;
const int IDSomeOtherRole = Qt::UserRole + 2;
const int IDNewRole = Qt::UserRole + 3; -
Thank you very much for your explanations and your support.