Important: Please read the 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


    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);
                       // 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 =;
                           QStringList record = current_index.split(",");
                           editor->setRelation(, QSqlRelation(,,;
                           columndelegateid <<;
                           columndelegatename <<;
                       // Setup table headers
                       QSqlQuery rcol;
                       rcol.exec("SELECT * FROM relationmap WHERE tbl = '"+table_name+"' ORDER BY UID ASC");
                       while (
                           editor->setHeaderData(result2.toInt(), Qt::Horizontal, result);
                       //Selecting the model
                       editor->setSort(0, Qt::AscendingOrder);
                       while (editor->canFetchMore())
                       for (int i = 0; i < columndelegateid.size(); i++)
                           QSqlQuery checktableexists;
                           if (!checktableexists.exec("SELECT Count(*) AS Count FROM """))
                               emit sendNotice("Error in query", ""+check+" does not exist");
                               ui->advanced_editor_table->setItemDelegateForColumn(, new QSqlRelationalDelegate(ui->advanced_editor_table));

  • 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