Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlRelationalTableModel questions
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel questions

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    udx6bs
    wrote on last edited by
    #1

    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

    Using Qt5.02/32bit on Windows Win7/64bit

    1 Reply Last reply
    0
    • F Offline
      F Offline
      franku
      wrote on last edited by
      #2

      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.

      This, Jen, is the internet.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        franku
        wrote on last edited by
        #3

        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?

        This, Jen, is the internet.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          franku
          wrote on last edited by
          #4

          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?

          This, Jen, is the internet.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            franku
            wrote on last edited by
            #5

            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(); @

            This, Jen, is the internet.

            1 Reply Last reply
            0
            • U Offline
              U Offline
              udx6bs
              wrote on last edited by
              #6

              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.

              Using Qt5.02/32bit on Windows Win7/64bit

              1 Reply Last reply
              0
              • kkmspbK Offline
                kkmspbK Offline
                kkmspb
                wrote on last edited by
                #7

                @udx6bs said in QSqlRelationalTableModel questions:

                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 ques

                Maybe you can see our free Qt project (4.8.1, Windows).

                This project contains several classes inherited from the base classes QSqlTableModel and QTableView.

                The PblSqlRelationalTableModel class is inherited from QSqlTableModel.

                The PblTableView class is inherited from QTableView.

                As a result, a combining class PblTableDlg was created with the composition PblTableView and PblSqlRelationalTableModel.

                The QTableView class contains full functionality for managing a database table: creating, copying, deleting rows.
                There is a search, selection by value. Fields in the form of combobox, checkbox,...
                External links and substitutions from other tabs work automatically.

                Everything is configured simply in one configuration file config.cpp .

                All three editing strategies are supported.

                github

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved