Important: Please read the Qt Code of Conduct -

[SOLVED] Memory access error with QSqlTableModel subclass

  • Solution: The problem was apparently a dependency issue caused by incorrect build. Re-building everything solved the issue. It seems that making full rebuild once in a while might be a good habit.

    I have a QSqlTableModel subclass that has two QList and one QString as private members. This is custom part of the model:

    @class OCMSqlTableModel : public QSqlTableModel
    explicit OCMSqlTableModel(QObject *parent = 0) : QSqlTableModel(QObject) { }

    void setMIMEType(const QString &table);
    void setDropSourceMap(const QList<int> &map)
    void disableEditingForColumn(int column);

    QString m_sMIMEType;
    QList<int> m_DropSourceTargetMap;
    QList<int> m_DisabledColumns;

    virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
    //rest of reimplemented stuff

    void OCMSqlTableModel::disableEditingForColumn(int column);

    Qt::ItemFlags OCMSqlTableModel::flags(const QModelIndex &index) const
    Qt::ItemFlags flags = QSqlTableModel::flags(index) | Qt::ItemIsDropEnabled;

            flags ^= Qt::ItemIsEditable;
        return flags | Qt::ItemIsDragEnabled;
    return flags;


    The problem is that like this it crashes in the flags() in the nested if checking whether the column is disabled (contained in the QList<int> private member).

    In debugging I found something super-odd. When this function is called, only the first QList is actually initialized while the second is inaccessible. When I swap them, everything works fine but the other QList is now inaccessible.

    What the heck is going on? Can someone please explain it? Or is it a bug?


  • Lifetime Qt Champion


    Probably not related but

    explicit OCMSqlTableModel(QObject *parent = 0) : QSqlTableModel(QObject) { }

    Should rather be

    explicit OCMSqlTableModel(QObject *parent = 0) : QSqlTableModel(parent) { }

    Or was it a typo ?

    Anyway, what combo of Qt/OS/compiler are you using ?

  • Yeah, it was a typo. :-)

    I am using Qt 5.3.2 with Ming 4.8.2 on Windows 8 64-bit.

  • Looks like a dependency/linking problem, make sure to recompile the entire project.

  • Ha, indeed you were right mmoll! How odd. Recompiling the whole thing fixed the issue and all members are properly initialized now.

Log in to reply