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 do I find a primary key using a QSqlRelationalTableModel?
QtWS25 Last Chance

how do I find a primary key using a QSqlRelationalTableModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlmodel
4 Posts 3 Posters 360 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.
  • J Offline
    J Offline
    jdent
    wrote on 14 Apr 2024, 11:37 last edited by
    #1

    if all I have is a QSqlRelationalTableModel, and want to retrieve a record with primary key X, how can I do it?

    Regards,
    Juan Dent

    C 1 Reply Last reply 14 Apr 2024, 11:41
    0
    • J jdent
      14 Apr 2024, 11:37

      if all I have is a QSqlRelationalTableModel, and want to retrieve a record with primary key X, how can I do it?

      Regards,
      Juan Dent

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Apr 2024, 11:41 last edited by
      #2

      @jdent said in how do I find a primary key using a QSqlRelationalTableModel?:

      how can I do it?

      By looking in the documentation and finding QSqlTableModel::primaryKey()

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

      J 1 Reply Last reply 14 Apr 2024, 11:46
      0
      • C Christian Ehrlicher
        14 Apr 2024, 11:41

        @jdent said in how do I find a primary key using a QSqlRelationalTableModel?:

        how can I do it?

        By looking in the documentation and finding QSqlTableModel::primaryKey()

        J Offline
        J Offline
        jdent
        wrote on 14 Apr 2024, 11:46 last edited by jdent
        #3

        @Christian-Ehrlicher No I wasn't clear. I know what the primary key of a table is. I need to find a record in that table with primary key == 5, for instance, using the model QSqlRelationalTableModel.
        I can always do a loop:

        	int located_row = -1;
        	for( int row =0; row < model->rowCount(); ++row)
        	{
        		auto id = model->record(row).value(0).toInt();
        		if(id == 2)
        		{
        			located_row = row;
        			break;
        		}
        	}
        

        but there must be a method!

        J 1 Reply Last reply 14 Apr 2024, 12:03
        0
        • J jdent
          14 Apr 2024, 11:46

          @Christian-Ehrlicher No I wasn't clear. I know what the primary key of a table is. I need to find a record in that table with primary key == 5, for instance, using the model QSqlRelationalTableModel.
          I can always do a loop:

          	int located_row = -1;
          	for( int row =0; row < model->rowCount(); ++row)
          	{
          		auto id = model->record(row).value(0).toInt();
          		if(id == 2)
          		{
          			located_row = row;
          			break;
          		}
          	}
          

          but there must be a method!

          J Offline
          J Offline
          JonB
          wrote on 14 Apr 2024, 12:03 last edited by JonB
          #4

          @jdent
          You have to iterate through the rows looking for the value you want.
          If you use a QSortFilterProxyModel you can take advantage of binary search for speed if you sort by primary key. QSqlRelationalTableModel is not important here, all that gives is a "lookup" on one field to map e.g. an integer value to a corresponding string from a related table for display.

          1 Reply Last reply
          2

          4/4

          14 Apr 2024, 12:03

          • Login

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