Results 1 to 2 of 2

Thread: How to move QDataWidgetMapper to a specific record ?

  1. #1
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to move QDataWidgetMapper to a specific record ?

    Hi,

    I have a small database application that allows maintaining the member list of a sports club.

    I have a form with QLineEdit's for the different fields. I have a QSqlTableModel to connect to the table. I use a QDataWidgetMapper to connect the linedits to the fields in the database.

    Like this :
    Qt Code:
    1. QSqlTableModel *model= new QSqlTableModel(this);
    2. model->setTable("MemberList");
    3. model->select();
    4.  
    5. mapper = new QDataWidgetMapper(this);
    6. mapper->setModel(model);
    7. mapper->addMapping(ui->leFirstName, model->fieldIndex("FirstName"));
    8. mapper->addMapping(ui->leSurName, model->fieldIndex("SurName"));
    To copy to clipboard, switch view to plain text mode 

    I can move back and forward using the navigation methods of the mapper :
    Qt Code:
    1. mapper->toNext();
    2. mapper->setCurrentIndex( /* any number here */ );
    3. }
    To copy to clipboard, switch view to plain text mode 

    But now I need to move to a specific record. For instance, the user can type a name in a filter box somewhere, and the corresponding record must be shown. (In reality I get a key value of the record when a member is selected in some other form in the application, and need to move to the record with that value in its 'MemberKey' field.)

    Unfortunately the mapper or model have no methods for searching for a specific record.

    I thought I could use setFilter() on the model, like below, but when I do this the program crashes.
    Qt Code:
    1. model->setFilter( QString(" FirstName= 'Peter' ") );
    To copy to clipboard, switch view to plain text mode 

    Does anyone know how to move to a specific record ? I have no idea of the index number of the record to show, so I cannot use setCurrentIndex().

    Best regards,
    Marc

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: How to move QDataWidgetMapper to a specific record ?

    OK... stupid mistake. The member variable for the model was not filled in, it was put into a local variable instead.

    For those interested : just do a setfilter() to the record you want, and then do a toFirst().

    Qt Code:
    1. model->setFilter( QString(" MemberKey = %1 ").arg(sKeyValue) );
    2. mapper->toFirst();
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Marc

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2010, 00:46
  2. Replies: 3
    Last Post: 4th August 2010, 18:51
  3. Replies: 3
    Last Post: 26th March 2010, 04:32
  4. QListView: How to move the cursor to a specific row
    By muellerp in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2008, 07:29
  5. how to move cursor to specific line in Text
    By showhand in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2006, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.