PDA

View Full Version : TableView/Model Display Question



SixDegrees
20th April 2010, 01:49
I'm implementing a qTableView/Model where I want some of the table cells to be editable, and others to be simply for display. I can control this with the flags() function. But I'd like the editable cells to use a different text color, so their editable status is visually apparent.What is the simplest way to accomplish this?

If the only way to do this is to subclass a custom delegate, is it possible to just set the brush in the paint() routine to the color I want (based on the model index) and then call the parent paint routine? Or will the parent perform a save()/restore() and wipe out my modifications?

I'm hoping such subclassing isn't necessary in the first place.

nikhilqt
20th April 2010, 07:48
Check out the stylesheet of QTableview, Depending on the editable and non-editable fields set the background color.. For Ref. http://doc.trolltech.com/4.6/stylesheet-examples.html#customizing-qtableview

SixDegrees
20th April 2010, 08:29
Thank you; I think that will work. I had already looked at stylesheets as a possible solution, but hadn't realized you could set arbitrary properties on them.

SixDegrees
20th April 2010, 11:23
Sadly, this simple and elegant approach won't work. The 'editable' property isn't exposed by QTableView, so apparently the only way to do this is to subclass ItemDelegate and tweak it's paint method. I'm hoping it won't involve anything more than changing the pen color and kicking the actual drawing back to the parent class.

nikhilqt
20th April 2010, 15:42
Ok. Can't you use Disable / Enable property. I think, these properties should be there. The fields which are not editable, make it disabled and try setting the stylesheet. I haven't given a try, check out once.

I have done something similar for QMenu, Find some snippets below regarding it. Hope it helps!


.QMenu::item:!disabled:selected { }


.QMenu::item:disabled, .QMenu::item:disabled:selected { }

bmhautz
20th April 2010, 16:52
You may not have to use a custom delegate. If you look at the documentation for the Qt::ItemDataRole, you'll notice that there is a Qt::ForegroundRole which you can specify in your model's data method to return whatever decoration you want. Since your model should know which fields are editable, then you can use this logic to return data in different QBrush values.

bmhautz
20th April 2010, 16:57
For example, you could have in your model's data method:



if( role == Qt::ForegroundRole && isEditable ) return QBrush(QColor(0, 0, 255)); //blue


This should be a less painful way to do what you want to do.

SixDegrees
20th April 2010, 19:53
This works beautifully, although it's a bit disquieting to be setting colors - which are essentially GUI items - down in the data model, which is normally concerned with...well, data, not presentation. But it solves my problem.

Now, if I could figure out how to keep the editable cell's contents from disappearing when they're clicked on, and stick around while the user modifies them, most of my table issues will be resolved.

Thanks again.

SixDegrees
20th April 2010, 20:03
All problems are now solved. I realized, after applying the last tip, that the data() function can also be called in an EditRole, which demands the return of the data in an editable form. Not doing this returns...nothing, so nothing is what replaces the cell's contents while editing.

franku
27th August 2010, 10:06
Thank you, so I am now able to highlight several "selected" Rows constantly out of the model:



if( role == Qt::ForegroundRole ) {
if(myElementDataContainer.at(index.row).isSelected ())
return QBrush(QColor(0, 0, 255)); //blue
}