Results 1 to 3 of 3

Thread: advice on using QDataWidgetMapper in QtSql

  1. #1
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default advice on using QDataWidgetMapper in QtSql

    I have two tables: address and unit
    unit table contains a field called 'AddressID'

    address table contains primary key 'AddressID'

    One address can occupy one ore more units.

    The unit datawidget mapper is configure like this:
    Qt Code:
    1. model = new QSqlTableModel;
    2. model->setTable("unit");
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. model->select();
    5.  
    6. mapper = new QDataWidgetMapper(this);
    7. mapper->setModel(model);
    8. mapper->addMapping(ui->le_Unit, model->fieldIndex("UnitName"));
    9. mapper->addMapping(ui->le_AddressID, model->fieldIndex("AddressID"));
    10. mapper->addMapping(ui->le_UnitID,model->fieldIndex("UnitID"));
    11.  
    12. connect(ui->pb_Prev,SIGNAL(clicked()),mapper, SLOT(toPrevious()));
    13. connect(ui->pb_Next,SIGNAL(clicked()),mapper, SLOT(toNext()));
    14. connect(mapper,SIGNAL(currentIndexChanged(int)),this,SLOT(mapperChanged(int)));
    To copy to clipboard, switch view to plain text mode 

    The Address data widget mapper is configured like this:

    Qt Code:
    1. addrmodel = new QSqlTableModel;
    2. addrmodel->setTable("addresses");
    3. addrmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. addrmodel->select();
    5. addrMapper = new QDataWidgetMapper(this);
    6. addrMapper->setModel(addrmodel);
    7. addrMapper->addMapping(ui->le_AID,addrmodel->fieldIndex("AddressID"));
    8. addrMapper->addMapping(ui->le_FirstName,addrmodel->fieldIndex("FirstName"));
    9. addrMapper->addMapping(ui->le_LastName,addrmodel->fieldIndex("LastName"));
    To copy to clipboard, switch view to plain text mode 

    In addition to this, I have a combo box where I have added all the unit name and unit id from the unit table so I can jump to any unit by calling mapperChanged with the index of the combo box.

    This all works great, I can jump to any unit. My goal is to show the matching address and the only way I can make it work is like this:

    Qt Code:
    1. for(int row = 0; row < addrmodel->rowCount(); row++)
    2. {
    3. QSqlRecord record = addrmodel->record(row);
    4. if(record.value(0).toInt() == ui->le_AddressID->text().toInt())
    5. {
    6. addrMapper->setCurrentIndex(row);
    7. break;
    8. }
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    However, this doesn't seem that efficient and I was hoping I could simply filter the addrmodel instead :

    Qt Code:
    1. addrmodel->setFilter("AddressID='" + ui->le_AddressID->text() + "'");
    To copy to clipboard, switch view to plain text mode 

    But the addrMapper doesn't update. What am I doing wrong?

    Is there a way to find the index of the model after the filter has been applied?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: advice on using QDataWidgetMapper in QtSql

    According to the QDataWidgetMapper docs:
    Every time the current index changes, each widget is updated with data from the model via the property specified when its mapping was made.
    I am guessing that changing (resetting) the underlying model is not changing the mapper's idea of the current index so it is not updating. Try a QDataWidgetMapper::toFirst() or QDataWidgetMapper::setCurrentIndex() to force it to think again.

  3. The following user says thank you to ChrisW67 for this useful post:

    schnitzel (5th June 2010)

  4. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: advice on using QDataWidgetMapper in QtSql

    Quote Originally Posted by ChrisW67 View Post
    According to the QDataWidgetMapper docs: I am guessing that changing (resetting) the underlying model is not changing the mapper's idea of the current index so it is not updating. Try a QDataWidgetMapper::toFirst() or QDataWidgetMapper::setCurrentIndex() to force it to think again.
    You are right, this seems to work fine:

    Qt Code:
    1. addrmodel->setFilter("AddressID='" + ui->le_AddressID->text() + "'");
    2. addrMapper->toFirst();
    To copy to clipboard, switch view to plain text mode 

    thanks for the tip

Similar Threads

  1. Replies: 4
    Last Post: 1st June 2010, 11:42
  2. advise sought on TreeView model design
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 25th June 2009, 14:36
  3. Need some advise with thumbnails and loading image
    By nmuntz in forum Qt Programming
    Replies: 2
    Last Post: 9th June 2009, 14:32
  4. Replies: 6
    Last Post: 25th February 2008, 21:18
  5. Replies: 8
    Last Post: 27th August 2007, 15:45

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.