PDA

View Full Version : set QTableView column to read-only in Python



nlgootee
24th June 2016, 19:41
I have searched the archives and I found the following code:


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;
}
};


Then use ManifestModel in place of QSqlTableModel.

This does exactly what I need to do, but I don't understand C++ syntax. Could someone please convert this to Python for me. I would really appreciate it.

jefftee
25th June 2016, 02:20
I won't do the literal translation for you, but I'll point you in the right direction:



ManifestModel is a class that subclasses QSqlTableModel
The ManifestModel has one method (function) that is overridden from the QSqlTableModel::flags method that take one argument, a QModelIndex
The method checks if the column in the QModelIndex is the 2nd column, it sets the flags to ItemIsEnabled + ItemIsSelectable + ItemIsEditable, otherwise it's just enabled and selectable (not editable)


After reading the documentation for QSqlTableModel and/or QAbstractItemModel, you should be able to figure it out, assuming you know how to create a class in python, etc.

Good luck.

nlgootee
25th June 2016, 13:28
I assume that you know that the documentation for pyqt is written in c++ and therefore is not really understandable for someone who does not know c++ syntax., or did you miss the part where I said that I don't know c++ syntax. Why did you bother to answer if you didn't want to actually help?

jefftee
25th June 2016, 18:22
Why did you bother to answer if you didn't want to actually help?
Heh, I thought I *was* actually helping you. I told you exactly what the C++ code was doing, so that you could write the same in Python. If you can't take that and run with it, then you are correct, I can't (or won't) help you.

Good luck.

nlgootee
30th June 2016, 22:14
OMG! A c++ programmer with an attitude. You're a real asset to the list.

anda_skoa
1st July 2016, 00:33
OMG! A c++ programmer with an attitude. You're a real asset to the list.
Yes, jefftee is an asset, a poster with the attitude to help people in need of advice.

Providing a translation of C++ code into general programming concepts, which a developer using a different language can easily translate into code again.

Cheers,
_