PDA

View Full Version : qtableview QStandardItemModel read only



JeanC
9th February 2008, 15:15
Hello,

How do I make a QTableView / QStandardItemModel read only?

With QSqlTableModle it's in edtiStrategy, however I can't find a similar thing in QStandardItemModel.

And a general question. A situation with a table and a delegate, how does one communicate between the two, for instance if the paint event in the delegate wants to know the current item in the table view.
For now I use global vars / functions for this, but I bet there is a more object oriented way to do that. :)

Greets, Jean.

marcel
9th February 2008, 15:23
Set QAbstractItemView::NoEditTriggers with QTableView::setEditTriggers.

For the second problem, you either make the selection model available in the delegate(an easy solution), or for every item you add some custom boolean data(for a user role) which tells you if the item is selected or not.


For the first solution, you can pass the view as the delegate's parent when you construct it. You can cast parent() inside the delegate back to the view and access its selection model.
For the second solution, you can get the data for your role in the delegate's paint and see if it is selected.

EDIT: I think you can use QStyleOptionViewItem::state variable. If it QStyle::State_selected, then the item is selected. I am not sure if it contains valid values when it is passed to you.

EDIT2: I just had a look at the QItemDelegate::paint implementation. It seems that QStyleOptionView::state is valid, so you can just test for QStyle::State_Selected.

JeanC
9th February 2008, 16:42
Thanks marcel,

I looked at QAbstractItemView, must have overlooked that.


Set QAbstractItemView::NoEditTriggers with
QTableView::setEditTriggers.

I used the first solution. I was using table->setItemDelegate(new myDelegate); because I read that in some example and when I first tried to change to a new'd class it didn't work so I thought that was the only way. Fortunately it does work otherwise.



For the first solution, you can pass the view as the delegate's parent when you construct it. You can cast parent() inside the delegate back to the view and access its selection model.


This one also looks good yes.,


EDIT: I think you can use QStyleOptionViewItem::state variable.

Great help again.