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. [SOLVED] Memory access error with QSqlTableModel subclass
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Memory access error with QSqlTableModel subclass

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.6k 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.
  • R Offline
    R Offline
    Resurrection
    wrote on last edited by
    #1

    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.

    Hi,
    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
    {
    Q_OBJECT
    public:
    explicit OCMSqlTableModel(QObject *parent = 0) : QSqlTableModel(QObject) { }

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

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

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

    void OCMSqlTableModel::disableEditingForColumn(int column);
    {
    m_DropSourceTargetMap.append(column);
    }

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

    if(index.isValid())
    {
        if(m_DisabledColumns.contains(index.column()))
            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?

    Thx.

    Secrets are power.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Resurrection
        wrote on last edited by
        #3

        Yeah, it was a typo. :-)

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

        Secrets are power.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mmoll
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Resurrection
            wrote on last edited by
            #5

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

            Secrets are power.

            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