Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSqlRelational ComboBox Delegate doesn't work with items more than 255



  • Qt 5.7.0:

    I have a relation set up whereby there are more than 255 items in the combobox delegate.

    Changing the data structure is /not/ an option as this is an interface for a database I haven't created.

    Essentially the problem is thus:

    • Set up relation delegate on column that has more than 255 items in the delegate
    • Change the item index in the QSqlRelationalTableView to any item after index 255.
    • The combobox reverts.
    • Similarly, if the current index of the delegate is > 255 and I enter the editor, the current index automatically changes to 0.

    Does anybody have any idea what solutions I could use for this?


  • Lifetime Qt Champion

    Hi,

    Can you show the code you are using for that to happen ?



  • The table referencing in table_name is the table that I have loaded from the db.
    A column may contain a range from 1-1000 (as an example), which I then use relationmap table to get the information I need (a relation table, the uid and name) to set up the itemDelegateforColumn.

    It works fine, for most things, but if I am using the delegate to convert ids for a whole bunch of person ids for example (which there can be hundreds), it just doesn't work when using the delegate.

    I am also using an Access DB for this, in case this information is of any use. I don't really have a choice to change that. Though I could see if its an issue using similar data in a SQL DB instead.

    Anyway, here's the relevant code, inside a function called setupTable(), which decides what table model type to use, depending on a user setting. (SqlTableModel or SqlRelationalTableModel).

    QSqlRelationalTableModel *editor = new QSqlRelationalTableModel(this);
                       editor->setTable(table_name);
                       editor->setEditStrategy(QSqlTableModel::OnFieldChange);
                       // Setup table relations.
                       QStringList columndelegateid;
                       QStringList columndelegatename;
                       for (int i = 0; i < relations.size(); i++) // relations is a QStringList comprising all relations for each column.
                       {
                           QString current_index = relations.at(i);
                           QStringList record = current_index.split(",");
                           editor->setRelation(record.at(0).toInt(), QSqlRelation(record.at(1), record.at(2), record.at(3)));
                           columndelegateid << record.at(0);
                           columndelegatename << record.at(1);
                       }
                       // Setup table headers
                       QSqlQuery rcol;
                       rcol.exec("SELECT * FROM relationmap WHERE tbl = '"+table_name+"' ORDER BY UID ASC");
                       while (rcol.next())
                       {
                           editor->setHeaderData(result2.toInt(), Qt::Horizontal, result);
                       }
                       rcol.clear();
                       //Selecting the model
                       editor->setSort(0, Qt::AscendingOrder);
                       while (editor->canFetchMore())
                       {
                           editor->fetchMore();
                       }
                       editor->select();
    
                       ui->advanced_editor_table->setModel(editor);
                       for (int i = 0; i < columndelegateid.size(); i++)
                       {
                           QSqlQuery checktableexists;
                           if (!checktableexists.exec("SELECT Count(*) AS Count FROM "+columndelegatename.at(i)+""))
                           {
                               emit sendNotice("Error in query", ""+check+" does not exist");
                           }
                           else
                           {
                               ui->advanced_editor_table->setItemDelegateForColumn(columndelegateid.at(i).toInt(), new QSqlRelationalDelegate(ui->advanced_editor_table));
                           }
    					   checktableexists.clear();
                       }
                       ui->advanced_editor_table->verticalHeader()->hide();
                       ui->advanced_editor_table->show();
    

  • Lifetime Qt Champion

    One thing that I find surprising is the call to select on "editor" after calling fetchMore.

    Also, you should consider using a bit more intermediate variables, your code, while not pretty big, is not verbose enough to be easy to follow.


Log in to reply