[Interest] Customizing QTableView

Murphy, Sean smurphy at walbro.com
Fri Jan 29 18:38:16 CET 2016


I’m trying to customize some parts of a QTableView and I’m getting a little lost in the responsibility of the view vs. model vs. proxy styles vs. some other class I haven't stumbled on yet.

Here’s our requirements that differ from what the default classes do:
1. Clicking on a row needs to toggle coloring the background of the row with a custom color or just the default table color
2. User should be able to drag and drop rows to reorder the table

I've had #1 working for a while, but #2 is a new requirement. I've been handling #1 in a model; maintaining a bool per row as to whether the row should be highlighted and then in myModel::data(const QModelIndex &currIndex, int role) with role==Qt::BackgroundRole returning a QVariant() if the row shouldn't be highlighted or QVariant(itemCustomQColor) if it should be highlighted.  So that worked just fine.

But as I'm working on the newly added requirement #2 I'm hitting some visual snags - I've got the reordering working just fine, it just doesn't look how I want. Originally I had the view's selection mode set to QAbstractItemView::NoSelection, but it appears that to get drag and drop working, that needs to be QAbstractItemView::SingleSelection. But as soon as I do that, clicking on a row to start a drag changes the row's background color to have the default selected blue color (on Windows at least) regardless of whether or not that row is supposed to be custom color highlighted as described in #1. I don't want that blue selection color ever: I'd like for the color to remain whatever it was before the drag started. I really don't have any need for the user to be able to select a row other than it appears that is a requirement for the drag and drop mechanics unless I'm missing something. I'm not sure what class I do that with? Is there a role for that in the model that I've missed, or do I need to do that via some other mechanism? I'm pretty sure I can't use a QPalette or stylesheet since the color changes on a row-by-row basis, but correct me if I'm wrong.

Also on my wish list for #2:
- I'd like for the drag pixmap to have a little transparency. Looking at the Draggable Icons Example (http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html) it looks like the spot to do that is going to be to inherit QTableView and customize mousePressEvent(), creating my own QDrag?
- I'd like for the drop indicator be a solid line in between existing rows. The only drop we're allowing is so that the user can reorder rows, so it makes no sense to drop on a row, only in between. But default the drop indicator seems to surround an individual cell, not an entire row, even when the table's selection behavior is QAbstractItemView::SelectRows. I found this example http://stackoverflow.com/questions/7596584/qtreeview-draw-drop-indicator that uses a QProxyStyle to change the indicator to be a box that surrounds the entire row when the drop is over a row, or a solid line if the drop is between rows. So that is close, but I want it to always be a solid line in between rows. I haven't had much time yet to play with it to see if it's easy for me to figure out how to collapse the box to be a line in the correct place.

Any hints?
Sean


More information about the Interest mailing list