PDA

View Full Version : How to make a column read-only in QSqlRelationalTableModel?



aekilic
24th May 2007, 23:23
Dear All

We have a QSqlRelationalTableModel, and we would like to make a column read-only? Is there any example codes that could help us?

Thanks!

AEK

wysota
25th May 2007, 00:51
Subclass the model and reimplement the flags() method to not return Qt::ItemIsEditable for items in the first column.

aekilic
26th May 2007, 17:22
Could you please help us a little with a example! We are a bit new to QT:(

wysota
26th May 2007, 17:39
class MyModel : public QSqlRelationalTableModel {
public:
MyModel(QObject *parent=0) : QSqlRelationalTableModel(parent){}
Qt::ItemFlags flags ( const QModelIndex & index ) const{
if(index.column()!=0)
return QSqlRelationalTableModel::flags(index);
return (QSqlRelationalTableModel::flags(index) & ~Qt::ItemIsEditable);
}
};

patrik08
26th May 2007, 17:40
Could you please help us a little with a example! We are a bit new to QT:(

i attach a sample at end of class cpp you find the mysql structure....

aekilic
26th May 2007, 18:06
Let me explain more in our .h we call relationaltable below,

QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
model->setTable("stokdakiler");
model->setFilter("stokid = " + Stokid);
model->setEditStrategy(QSqlRelationalTableModel::OnFieldC hange);
model->setRelation(2, QSqlRelation("marka", "markaid", "marka"));
model->setRelation(4, QSqlRelation("birimler", "birimid", "birim"));
model->select();

table->setModel(model);
table->resizeColumnsToContents ();
table->resizeRowsToContents ();
table->setColumnHidden (0, true);
table->setColumnHidden (1, true);
table->setItemDelegate(new QSqlRelationalDelegate(tablestokda));
table->setSortingEnabled (true);

And after this what should we do?

wysota
26th May 2007, 18:51
You should substitute all occurences to QSqlRelationalTableModel with "MyModel" (based on the code I have given you - if you change the class name, change it here as well).

patrik08
26th May 2007, 18:53
Let me explain more in our .h we call relationaltable below,
And after this what should we do?

Build it..

other crude sample....
http://www.qtcentre.org/forum/f-qt-software-16/t-mysql-5-table-qt-model-as-small-mysql-admin-6807.html

aekilic
28th May 2007, 21:32
Thank you very much both of you, we were able to solve the problem easly!