QSqlRelationalTableModel questions



  • Hello there,
    I'm fairly new to Qt and trying to experiment with QSqlRelationalTableModel and QTableView. I'm using the SQLite driver and Qt 4.8 compiled(32bit) on Win7/64bit.
    I have the following questions:

    1.) My QSqlRelationalTableModel class constructor look like this:
    @CItemListModel::CItemListModel(){
    setTable("items");
    QSqlRelation q = QSqlRelation("types", "id", "title_en-US");
    setRelation(fieldIndex("type"), q);
    select();
    }@

    whenever i try to set my QTableViews model to this via "setModel()" my program exits with:

    ASSERT: “idx >= 0 && idx < s” in file ....\include/QtCore/../../src/corelib/tools/qvarlengtharray.h, line 110
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.

    if i change "title_en-US" to a field that doesn't contain a minus sign everything works smoothly.
    Is this a bug in Qt, or did i miss something?

    2.) If i call "q.isValid()" it returns true. In fact it always returns true even if i pass incorrect data (nonexistent table/columns) to the constructor of QSqlRelation's "aTableName", "indexCol" and/or "displayCol" parameters. Is this how it should work?

    3.) References to multiple tables:
    If i have for example the following tables:

    @CREATE TABLE types(
    id INTEGER PRIMARY KEY,
    titleEN VARCHAR(100),
    titleFR VARCHAR(100),
    ...
    );

    CREATE TABLE item(
    id INTEGER PRIMARY KEY,
    ...
    );

    CREATE TABLE itemTypes(
    id INTEGER REFERENCES item(id) ON UPDATE CASCADE ON DELETE CASCADE,
    order INTEGER(2),
    type INTEGER REFERENCES types(id) ON UPDATE CASCADE ON DELETE CASCADE,
    PRIMARY KEY(id, order)
    );
    @

    I want to have multiple "types" assigned to an "item" and have a Language code selection via a QCombobox separately (outside of my QTableView) so that only one Language can be shown/edited at a time.

    Is this somehow possible with QSqlRelationTableModel possible?

    4.) If i use "setQuery()" instead of "setTable()" in my QSqlRelationTableModel sorting via QTableView's horizontalHeader is not working anymore. I could use a Proxy for sorting but then i have to fetch all data into the model first before it becomes accurate. Is there a better way to do this?

    My english is not the best so sorry if something is not accurate.

    Thx in advance



  • 1.) Did you try separately to read the contents of your tables using a QSqlQuery? You may see more information on how the data is converted out of the database or even if there is any unresolved foreign key.



  • 3.) Try to explain exactly: What is the user going to choose from the ComboBox and which data do you want to derive from that information out of/into the database?



  • 4.) The model does all the database SELECTs for you, so the database table name has to be set. What do you want to achieve with your own query and why is the proxy the worse way?



  • 2.) QSqlRelation::isValid() tells you something about this object only. In this case it means that the TableName, indexCol and displayCol Parameters are not empty.

    You may want to try:
    @QSqlRelation q = QSqlRelation("types", "id", QString());
    qDebug() << q.isValid(); @



  • Thx for the reply,
    sorry for the delay was not at my computer for some time.

    1.) Did you try separately to read the contents of your tables using a QSqlQuery? You may see more information on how the data is converted out of the database or even if there is any unresolved foreign key.

    Yes, getting the data with QSqlQuery works perfectly, and no unresolved foreign keys for sure.

    Concerning the other questions:

    I gave up on QSqlRelationTableModel and now use a QSqlQueryModel for my table since i only have to reimplement setData, flags, sorting, filtering, which was very easy.


Log in to reply
 

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