QSortFilterProxyModel over QSqlRelationalTableModel : insertingColumn(s) snippet needed



  • Hi all!
    i have the issue on the title trying to insert 3 columns (only displayed) using QSortFilterProxyModel on a QTableView feeded by a QSqlRelationalTableModel.
    I'm looking for a snippet for subclassing and using the QSFPM correctly because i can't make it work in any situation : the columns never appear.

    @m_proxyModel = new DossierProxyModel(this);
    m_proxyModel->setSourceModel(m_mainModel);
    m_proxyModel->setDynamicSortFilter(true);

    bool ok = m_proxyModel->insertColumns(3, 3); // ok is always true

    ui->tvDList->setModel(m_proxyModel);@

    m_proxyModel is a (almost empty) subclassed QSortFilterProxyModel that (at the moment) doesn't alter the source model displayed content.

    m_mainModel (a QSqlRelationalTableModel) without the proxy works fine and i can display and edit data synchronizing with a MySQL db.

    Before implementing a correct proxy model, shouldn't i see the new 3 columns appearing on tableview ?

    Do i need to implement my own insertColumns method in the proxy?

    I searched everywhere on the web and i didn't find anything about a complete tutorial/example/snippet about subclassing a QSortFilterProxyModel for columns insertion and i don't know how to finish the work.

    I'm working with a 4.7 and i'm trying to improve my Qt usage.

    Thank you in advance for any hint



  • [quote author="pasomas" date="1333752749"]Hi all!
    Before implementing a correct proxy model, shouldn't i see the new 3 columns appearing on tableview?[/quote]
    No. the insertColumn method only works for models that actually support that operation. Your models do not, so the operation is basically void.

    [quote]
    Do i need to implement my own insertColumns method in the proxy?[/quote]
    Yes, that's what it comes down to.

    [quote]
    I searched everywhere on the web and i didn't find anything about a complete tutorial/example/snippet about subclassing a QSortFilterProxyModel for columns insertion and i don't know how to finish the work.

    I'm working with a 4.7 and i'm trying to improve my Qt usage.[/quote]

    I guess it all depends on what it is you really want to do. Do you just want to add a couple of columns that display some data derived from the information in the other colums, or do you really want to add columns to your source database? It seems you want to do the former, but could you try to explain your exact goal a bit better anyway please?



  • Thank you Andre for your reply.
    I realized my question was not well exposed.
    The answer is near the former situation but taking data from another table, not from the other columns.
    My goal is to display 3 further read-only columns to the user without modifying the database.

    I have a table Dossier that the user manages completely through a QTableView, a QSqlRelationalTableModel and a QDataWidgetMapper.
    I have this :

    Dossier | Dossier Description | Customer Code | Dossier Type

    and i just want to achieve this

    Dossier | Dossier Description | Customer Code | Customer Business Name | Customer Name | Customer Surname | Dossier Type

    Customer related new fields should be read from the relational table "Customer" by the key "Customer Code".

    I would ask you what should be the correct approach and an example/snippet if possible.



  • Sounds like you should solve that by simply modifying your SQL query...



  • Ok, so am i in the situation described by "this old thread":http://qt-project.org/forums/viewthread/2862 ?
    I'll try redefine the select statament or maybe better setting it through the setQuery in my subclassed QSqlRelationalModel.

    I thought there was an "higher level solution"...i have a main table and 4 relations to other tables and the statement is not small. :)

    Thank you Andre, if i'll make it work i'll mark the thread as resolved.



  • I changed all the code to work with QSqlQueryModel and i removed the QDataWidgetMapper.
    The code is pretty longer but now is more flexible and it works fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.