Results 1 to 5 of 5

Thread: setRelation and NULL in ID

  1. #1
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy setRelation and NULL in ID

    PROBLEM:
    In a QTableView, if a row has a null value in a combo box field (that writes in the row the value of a foreign key), the user does not see the row (!).

    MORE INFORMATION:
    I need to have a form where the user enters information about something in a QTableView (for the sake of clarity, I have chosen an example of a database of poems).

    Some poems would have author and others not; (the author would be the foreign key). if I try a code like this:

    Qt Code:
    1. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    2. model.setTable("poems");
    3. model.setRelation(3, QSqlRelation( "authors", "author_id", "name"));
    4. model.setHeaderData(0, Qt::Horizontal, t("ID"));
    5. model.setHeaderData(1, Qt::Horizontal, t("Title"));
    6. model.setHeaderData(2, Qt::Horizontal, t("Year"));
    7. model.setHeaderData(3, Qt::Horizontal, t("Author"));
    8. model.select();
    9.  
    10. QTableView table_view;
    11. table_view->setModel(model);
    12. table_view->setItemDelegate(new QSqlRelationalDelegate(table_view));
    To copy to clipboard, switch view to plain text mode 

    Then the rows that are poems without an author... are not shown.
    Auryn
    Starting to learn the world of Qt

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setRelation and NULL in ID

    The problem is that Qt uses inner join and you want to use outer join, so instead you can use QSqlTableModel (if you have a view) or QSqlQueryModel. In the latter case the model won't be editable and with view it depends on the underlying DBMS.

  3. #3
    Join Date
    Aug 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setRelation and NULL in ID

    I am also interested in this problem. But I dont understand the answer from jacek. Have someone solved this problem? Thanks.

  4. #4
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setRelation and NULL in ID

    By now, I will use a record with identifier 0 and empty description in my tables.

    See this too:
    http://www.qtcentre.org/forum/f-qt-p...del-12784.html
    Auryn
    Starting to learn the world of Qt

  5. #5
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: setRelation and NULL in ID

    Hi,

    I know this is an old thread, but you need to set the setJoinMode. By default the sqlRelationalTableModel uses an inner join. You need to set it to be a left join.
    self.setJoinMode(QtSql.QSqlRelationalTableModel.Le ftJoin). This took me forever to figure out because the row would go into the database just not the view.

    http://pyqt.sourceforge.net/Docs/PyQ...ml#setJoinMode

Tags for this Thread

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.