PDA

View Full Version : Column Not Editable



waynew
1st August 2010, 00:18
I have a QSqlTableModel and a QTableView.
Trying to make just one column not editable.
Found several examples in previous posts, but nothing is making sense to me as how to accomplish this in my code.

The signal/slot for when a cell is clicked is working ok.

Here is what I have so far:



void MainWindow::checkEditable(QModelIndex index) {
qDebug() << "view has been clicked!"; // good so far
int col = index.column();
int row = index.row();
qDebug() << "clicked column is " << col; // got the column ok
if (col != 0) { // it should be ok to edit
// now what???
}
}


Am I on the wrong track or is this simple to finish?

norobro
1st August 2010, 01:43
Hi Waynew,
An easy way to do this is to subclass QSqlTableModel. All you have to do is reimplement QSqlTableModel::flags(). If you are using Qt Creator add a new C++ class, put the flags() declaration in the header file and put the following in the cpp file:
Qt::ItemFlags YourCustomModel::flags( const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlTableModel::flags(index);
if (index.column() == 1 )
flags &= ~Qt::ItemIsEditable;
return flags;
}

Change the "1" to the column you want to not be editable.

HTH