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. How to get index[int] of current selected row from ListView in Qt with C++?

How to get index[int] of current selected row from ListView in Qt with C++?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 4.1k Views
  • 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.
  • T Offline
    T Offline
    time
    wrote on last edited by time
    #1

    I am trying to delete row in my database, through Qt ListView. So in GUI window I have listView with some data. User select one row and after clicking 'delete button', the row is deleting from ListView and database. I have problem with getting the number of the row which the user selected. The program returning me value -1

    void MainWindow::on_pushButtonUsunWydatek_clicked()
    {
    connectionOpen(); // Open connection to the DB
    QSqlQueryModel model;

    int index = ui->listView_Wydatki->currentIndex().row(); //want to get current selected row by the user HERE is the main problem
    
    int id = model.record(index).value("id").toInt(); //here I collect specific ID from Database
    
    //here I check if it get well
    qDebug() << "ID: " << id;
    qDebug() << "Index: " << index;
    
    model.setQuery("DELETE FROM wydatki WHERE id = "id" "); // additional question: how to properly put 'id' variable here?
    
    connectionClose(); //closing connection with DB 
    

    }

    raven-worxR 1 Reply Last reply
    0
    • T time

      I am trying to delete row in my database, through Qt ListView. So in GUI window I have listView with some data. User select one row and after clicking 'delete button', the row is deleting from ListView and database. I have problem with getting the number of the row which the user selected. The program returning me value -1

      void MainWindow::on_pushButtonUsunWydatek_clicked()
      {
      connectionOpen(); // Open connection to the DB
      QSqlQueryModel model;

      int index = ui->listView_Wydatki->currentIndex().row(); //want to get current selected row by the user HERE is the main problem
      
      int id = model.record(index).value("id").toInt(); //here I collect specific ID from Database
      
      //here I check if it get well
      qDebug() << "ID: " << id;
      qDebug() << "Index: " << index;
      
      model.setQuery("DELETE FROM wydatki WHERE id = "id" "); // additional question: how to properly put 'id' variable here?
      
      connectionClose(); //closing connection with DB 
      

      }

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @time said in How to get index[int] of current selected row from ListView in Qt with C++?:

      what does the following output?
      qDebug() << model.record(index).value("id")

      model.setQuery("DELETE FROM wydatki WHERE id = "id" "); // additional question: how to properly put 'id' variable here?

      There are multiple possible solutions. One can be model.setQuery( QString("DELETE FROM wydatki WHERE id = \"") + id + "\"" )

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      T 1 Reply Last reply
      1
      • raven-worxR raven-worx

        @time said in How to get index[int] of current selected row from ListView in Qt with C++?:

        what does the following output?
        qDebug() << model.record(index).value("id")

        model.setQuery("DELETE FROM wydatki WHERE id = "id" "); // additional question: how to properly put 'id' variable here?

        There are multiple possible solutions. One can be model.setQuery( QString("DELETE FROM wydatki WHERE id = \"") + id + "\"" )

        T Offline
        T Offline
        time
        wrote on last edited by time
        #3

        @raven-worx Thank you. And what with other problems? Especially with getting index of the selected row in ListView?
        I can't get the output qDebug() << model.record(index).value("id") because I don't have properly index. The program returning index -1

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          ui->listView_Wydatki->selectionModel()->selectedIndexes() will return a list of selected indexes. do not use currentIndex() as if you deselect an item current index will be that item

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

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