Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget same name's with different id's from sqlite query should be shown in qlineedit by clicking the item
Forum Updated to NodeBB v4.3 + New Features

QListWidget same name's with different id's from sqlite query should be shown in qlineedit by clicking the item

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 626 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      ? 1 Reply Last reply
      1
      • ? A Former User

        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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 what QListWidget is derived from. Use the model for the QListView, and pick the name column for what it should display.

        • Maybe change to a QTableView, which shows multiple columns against each row, unlike a QListView. 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.

        1 Reply Last reply
        2
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          • mrjjM mrjj

            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.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @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 :)

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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 up

                const int IDRole = Qt::UserRole + 1;
                const int IDSomeOtherRole = Qt::UserRole + 2;
                const int IDNewRole = Qt::UserRole + 3;

                1 Reply Last reply
                1
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Thank you very much for your explanations and your support.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved