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

QSqlTableModel and flags (subclass)



  • Hi!

    I try to get the data from a Sqlite-database and put it into a QSqlTableModel, but column no.1 should not be editable. It has five columns and more than 200 rows.

    As far as I found out from previous answers, I have to subclass flags, which I tried.
    However, when I start my application, all columns are editable, selectable, etc.

    This is my code:
    .cpp - file:

    QSqlTableModel *model = new QSqlTableModel(this,make_db_call.db);
        model->setTable("my_table");
        model->select();
    
        ui->tableView->setModel(model);
        ui->tableView->show();
    
    QModelIndex myindex = ui->tableView->model()->index(1,1,QModelIndex());
    qDebug() << model->flags(myindex);
    

    subclass .h:

    #ifndef SUBCLASS_FLAGS_H
    #define SUBCLASS_FLAGS_H
    
    #include <QSqlTableModel>
    #include <QDebug>
    
    class subclass_flags : QSqlTableModel
    {
        Q_OBJECT
    public:
        subclass_flags();
        virtual Qt::ItemFlags flags(const QModelIndex & index) const;
    };
    
    #endif // SUBCLASS_FLAGS_H
    

    subclass .cpp

    #include "subclass_flags.h"
    
    subclass_flags::subclass_flags()
    {
    }
    
    Qt::ItemFlags subclass_flags::flags(const QModelIndex &index) const
    {
        Qt::ItemFlags flags = QSqlTableModel::flags(index);
           if (index.column() == 1 ) {
               flags = flags &(~Qt::ItemIsEditable);
           }
           else {
               flags = (Qt::ItemIsSelectable | Qt::ItemIsEditable);
           }
           return flags;
    qDebug() << "subclass successful";
    }
    

    The debug-result from the .cpp-file is always: QFlagsQt::ItemFlag(ItemIsSelectable|ItemIsEditable|ItemIsEnabled|ItemNeverHasChildren)

    In my .cpp-file I included "#include "subclass_flags.h""
    How can I find out if this works - because "subclass successful" never comes.

    Thanks for your help.



  • Seems you didn't use the subclass. Include the header file doesn't mean you use the class.

    QSqlTableModel *model = new subclass_flags(this, make_db_call.db);
    

    also the constructor of the subclass need to have the same parameters with QSqlTableModel.
    subclass.h:

    subclass_flags(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
    

    subclass.cpp:

    subclass_flags::subclass_flags(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent, db)
    {
    }
    


  • Seems you didn't use the subclass. Include the header file doesn't mean you use the class.

    QSqlTableModel *model = new subclass_flags(this, make_db_call.db);
    

    also the constructor of the subclass need to have the same parameters with QSqlTableModel.
    subclass.h:

    subclass_flags(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
    

    subclass.cpp:

    subclass_flags::subclass_flags(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent, db)
    {
    }
    


  • @Bonnie Thanks! Your solution works.

    Additionally I had to change
    class subclass_flags : QSqlTableModel
    to
    class subclass_flags : public QSqlTableModel - because I got an error message ("Cannot cast “derived” to its private base class").


Log in to reply