Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

How to make some cells not editable in a qtableview with model?



  • Hi to all.

    I am developping an application in wich I use a QTableview using a model to use a sqlite database.

    First I create the table:

    void syseditor::createTable() {
        tableview = new QTableView;
        tableview->setMinimumWidth(600);
        tableview->setMinimumHeight(400);
        tableview->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        connect(tableview->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(tableSelectionSlot(int)));
    
        tableview->setStyleSheet(QString::fromUtf8(""
            "QScrollBar:vertical {"
            "   border: 1px solid #999999;"
            "   background:white;"
            "   width:10px;    "
            "   margin: 0px 0px 0px 0px;"
            "}"
            "QScrollBar::handle:vertical {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "   stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130), stop:1 rgb(32, 47, 130));"
            "   min-height: 0px;"
            "}"
            "QScrollBar::add-line:vertical {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "   stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130),  stop:1 rgb(32, 47, 130));"
            "   height: 0px;"
            "   subcontrol-position: bottom;"
            "   subcontrol-origin: margin;"
            "}"
            "QScrollBar::sub-line:vertical {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "   stop: 0  rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130),  stop:1 rgb(32, 47, 130));"
            "   height: 0 px;"
            "   subcontrol-position: top;"
            "   subcontrol-origin: margin;"
            "}"
            ""
            "QScrollBar:horizontal  {"
            "   border: 1px solid #999999;"
            "   background:white;"
            "   height: 10px;"
            "   margin: 0px 0px 0px 0px;"
            "}"
            "QScrollBar::handle:horizontal  {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "   stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130), stop:1 rgb(32, 47, 130));"
            "   min-height: 0px;"
            "}"
            "QScrollBar::add-line:horizontal  {"
            "   background: qlineargradient(x1:0, y1:1, x2:0, y2:0,"
            "   stop: 0 rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130),  stop:1 rgb(32, 47, 130));"
            "   height: 0px;"
            "   subcontrol-position: left;"
            "   subcontrol-origin: margin;"
            "}"
            "QScrollBar::sub-line:horizontal  {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "   stop: 0  rgb(32, 47, 130), stop: 0.5 rgb(32, 47, 130),  stop:1 rgb(32, 47, 130));"
            "   height: 0 px;"
            "   subcontrol-position: top;"
            "   subcontrol-origin: margin;"
            "}"
        ));
    }
    
    

    Then I create the model, as follow:

    void syseditor::createModel(const QString tableName) {
        model = new QSqlTableModel();
    
        model->setTable(tableName);
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->select();
    
        model->setHeaderData(0, Qt::Horizontal, "ID");
        model->setHeaderData(1, Qt::Horizontal, "Id Dia");
        model->setHeaderData(2, Qt::Horizontal, "Dia");
        model->setHeaderData(3, Qt::Horizontal, "Início");
        model->setHeaderData(4, Qt::Horizontal, "Fim");
        model->setHeaderData(5, Qt::Horizontal, "Programa");
        model->setHeaderData(6, Qt::Horizontal, "Classificação");
        model->setHeaderData(7, Qt::Horizontal, "Apresentador");
    
        tableview->setModel(model);
        tableview->resizeColumnsToContents();
    
        tableview->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
        tableview->horizontalHeader()->setMinimumWidth(600);
        tableview->setAlternatingRowColors(true);
    
    
        connect(tableview->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
    }
    
    

    The problem is that I need to make columns 0,1 and 2 not editable.

    I have found some code as this:

    class ManifestModel: public QSqlTableModel
    {
    public: 
        ManifestModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
            QSqlTableModel(parent, db)
        { }
     
        ~ManifestModel() { }
     
        Qt::ItemFlags flags ( const QModelIndex & index ) const 
        {
            if (index.column() == 2)
                return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
            else
                return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        }
    };
    

    But I don't know hot to implement it (where to put it - some class, another file...).

    Or there is another solution?

    Could someone be kind to help me with this matter?
    I am still learning Qt and thanks for any help.


  • Lifetime Qt Champion

    Hi
    The function Flags() is used by the view to see if an item is selectable/editable etc.
    So subclassing QSqlTableModel is a good way to do it.

    Just take whole class declartion and put into mainwindow.h or where you normally use /have
    your QSqlTableModel. (in a .h file, maybe syseditor.h)

    Then create a ManifestModel instead of QSqlTableModel ( simply replace it )
    and give it to the view like normal.
    Then it should just work.

    void syseditor::createModel(const QString tableName) {
        model = new ManifestModel();
    ..
    and also 
    in .h where you have
    QSqlTableModel *model; -> ManifestModel *model;
    

    You will need to alter the ifs to include more columns.

    I hope its more clear now.


  • Lifetime Qt Champion

    Hi,

    An alternative that might also be interesting is QIdentityProxyModel where you re-implement the flags method.


Log in to reply