Results 1 to 7 of 7

Thread: Set a filter on a model/view

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Set a filter on a model/view

    Hi,

    I have this model:
    Qt Code:
    1. void CidadaoDialog::construirModeloCidadao()
    2. {
    3. modeloCidadao->setTable("Cidadao");
    4. modeloCidadao->setRelation(cidadao_localidade, QSqlRelation("Localidade", "idLocalidade", "nome"));
    5. modeloCidadao->setHeaderData(cidadao_idCidadao, Qt::Horizontal, tr("B.I."));
    6. modeloCidadao->setHeaderData(cidadao_nome, Qt::Horizontal, trUtf8("Nome"));
    7. modeloCidadao->setHeaderData(cidadao_localidade, Qt::Horizontal, trUtf8("Localidade"));
    8. modeloCidadao->setEditStrategy(QSqlRelationalTableModel::OnRowChange);
    9. modeloCidadao->setSort(cidadao_nome, Qt::AscendingOrder);
    10. modeloCidadao->select();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Used by this view:
    Qt Code:
    1. void CidadaoDialog::construirVistaCidadao()
    2. {
    3. vistaCidadao = ui->tableView;
    4. vistaCidadao->setModel(modeloCidadao);
    5. vistaCidadao->setItemDelegate(new QSqlRelationalDelegate(vistaCidadao));
    6. vistaCidadao->setSelectionMode(QAbstractItemView::SingleSelection);
    7. vistaCidadao->setSelectionBehavior(QAbstractItemView::SelectRows);
    8. vistaCidadao->setFixedWidth(605);
    9. vistaCidadao->verticalHeader()->hide();
    10. vistaCidadao->setColumnWidth(cidadao_idCidadao, 100);
    11. vistaCidadao->setColumnWidth(cidadao_nome, 300);
    12. vistaCidadao->setColumnWidth(cidadao_localidade, 200);
    13. }
    To copy to clipboard, switch view to plain text mode 

    If i set a filter like this ...
    Qt Code:
    1. void CidadaoDialog::filtrar_nome()
    2. {
    3. QString filtro;
    4. filtro = "nome like '%"+ui->filtroNome->text()+"%'";
    5. modeloCidadao->setFilter(filtro);
    6. modeloCidadao->select();
    7. }
    To copy to clipboard, switch view to plain text mode 
    ... i get no records displyed on the view.

    If i comment line 4 on the model code it works as expected, displaying all record tha match the texte entered on ui->filtroNome (lineEdit Widget).

    What am i missing here?
    Thanks

  2. #2
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set a filter on a model/view

    Well!
    Looks like i had two fields with the same name in the original table, as well as in the referenced table.
    I thinks this showld not be a problem ... but it is!
    Attached Images Attached Images

  3. #3
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Set a filter on a model/view

    So, is your cidadao_localidade value is correct? Looking at your diagram it should be 2.

    Try to identify your sql query:

    add this:

    Qt Code:
    1. qDebug() << modeloCidadao->query().lastQuery();
    To copy to clipboard, switch view to plain text mode 

    after:

    Qt Code:
    1. modeloCidadao->select();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to saa7_go for this useful post:

    graciano (10th June 2010)

  5. #4
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set a filter on a model/view

    So, is your cidadao_localidade value is correct? Looking at your diagram it should be 2.
    Yes.
    If filtering names containing "P" ...
    The last Query, in the working version gives me:
    Qt Code:
    1. "SELECT Cidadao.`idCidadao`, Cidadao.`nome`, relTblAl_2.nomeLocalidade FROM Cidadao, Localidade relTblAl_2 WHERE (Cidadao.`localidade` = relTblAl_2.idLocalidade) AND (nome like '%P%') ORDER BY Cidadao.`nome` ASC"
    To copy to clipboard, switch view to plain text mode 
    The not working version:
    Qt Code:
    1. "SELECT Cidadao.`idCidadao`, Cidadao.`nome`, relTblAl_2.nome AS Localidade_nome_2 FROM Cidadao, Localidade relTblAl_2 WHERE (Cidadao.`localidade` = relTblAl_2.idLocalidade) AND (nome like '%P%') ORDER BY Cidadao.`nome` ASC"
    To copy to clipboard, switch view to plain text mode 
    Aparently an alias is created when the duplicate name is detected (relTblAl_2.nome AS Localidade_nome_2) but, once again, it showld not be a reason not to work as expected ... i think !?

  6. #5
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Set a filter on a model/view

    maybe you need to be more specific when setting a filter in QSqlRelationalTableModel.

    modify your filter:

    Qt Code:
    1. filtro = "nome like '%"+ui->filtroNome->text()+"%'";
    To copy to clipboard, switch view to plain text mode 

    to:

    Qt Code:
    1. filtro = QString("%1.nome like '\%%2\%'").arg(modeloCidadao->tableName()).arg(ui->filtroNome->text());
    To copy to clipboard, switch view to plain text mode 

  7. The following 2 users say thank you to saa7_go for this useful post:

    graciano (11th June 2010), igorbrp (31st August 2010)

  8. #6
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set a filter on a model/view

    maybe you need to be more specific when setting a filter in QSqlRelationalTableModel.
    You are right about this.
    From now on i will allways use TableName.FieldName.
    This way all ambiguity will desapear.

    Anyway ... QSqlRelationalTableModel class showld be able to deal with this because, looking at the query generated by the model...
    Qt Code:
    1. "SELECT Cidadao.`idCidadao`, Cidadao.`nome`, relTblAl_2.nome AS Localidade_nome_2 FROM Cidadao, Localidade relTblAl_2 WHERE (Cidadao.`localidade` = relTblAl_2.idLocalidade) AND (nome like '%p%') ORDER BY Cidadao.`nome` ASC"
    To copy to clipboard, switch view to plain text mode 
    ... i realise it's performing some kind of join, without considering the possibility of fiels with the same name in the related tables.

  9. #7
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set a filter on a model/view

    OK ... i have nothing better to do

    I created a more readable model that goes like this:
    Qt Code:
    1. mysql> describe TableA;
    2. +---------+-------------+------+-----+---------+-------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +---------+-------------+------+-----+---------+-------+
    5. | field01 | int(11) | NO | PRI | 0 | |
    6. | field02 | varchar(45) | NO | | NULL | |
    7. | field03 | int(11) | NO | MUL | NULL | |
    8. +---------+-------------+------+-----+---------+-------+
    9. 3 rows in set (0.01 sec)
    10.  
    11. mysql> describe TableB;
    12. +---------+-------------+------+-----+---------+-------+
    13. | Field | Type | Null | Key | Default | Extra |
    14. +---------+-------------+------+-----+---------+-------+
    15. | field01 | int(11) | NO | PRI | NULL | |
    16. | field02 | varchar(45) | NO | | NULL | |
    17. +---------+-------------+------+-----+---------+-------+
    18. 2 rows in set (0.00 sec)
    19.  
    20. mysql> select TableA.field01, TableA.field02, TableB.field02 from TableA, TableB where TableA.field03 = TableB.field01 and field02 like "%p%";
    21. ERROR 1052 (23000): Column 'field02' in where clause is ambiguous
    22. mysql> select TableA.field01, TableA.field02, TableB.field02 from TableA, TableB where TableA.field03 = TableB.field01 and TableA.field02 like "%p%";
    23. +---------+---------+---------+
    24. | field01 | field02 | field02 |
    25. +---------+---------+---------+
    26. | 2 | pedro | ? |
    27. +---------+---------+---------+
    28. 1 row in set (0.00 sec)
    29.  
    30. mysql>
    To copy to clipboard, switch view to plain text mode 

    As said above, the use of the Tablename eliminates the "ambiguous" caused error.
    Using this database with my qt example, the generates query when filtering is:
    Qt Code:
    1. SELECT TableA.`field01`, TableA.`field02`, relTblAl_2.field02 AS TableB_field02_2 FROM TableA, TableB relTblAl_2 WHERE (TableA.`field03` = relTblAl_2.field01) AND (TableA.field02 like '%p%') ORDER BY TableA.`field01` ASC
    To copy to clipboard, switch view to plain text mode 

    So ... what is this relTblAl_2 doing in the query?
    It is used in the Qt part of the Query ... but the the programmer is allowed to enter an ambiguous fieldname?!
    Showld't this be fixed?

Similar Threads

  1. How to filter duplicates from a proxy model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 14th October 2016, 10:21
  2. Replies: 0
    Last Post: 21st April 2010, 12:23
  3. Replies: 1
    Last Post: 1st February 2010, 18:42
  4. QSql Model-View: How to keep view in sync with model
    By schall_l in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2008, 23:31
  5. Filter Proxy Model to Autoupdate View
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2006, 20:32

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.