PDA

View Full Version : QTableView formatting table



Kumosan
12th November 2009, 23:07
Hi, ich have the following proglem. I have a QTableView and add a model. Naturally. Now I get a table:


xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx

Fine this is what is expected and usually wanted.
But I want to have a bigger space every two rows:


xxxxxxx
xxxxxxx

xxxxxxx
xxxxxxx

xxxxxxx
xxxxxxx

Anyone an idea? I tried to use setRowHeight. Works great...as long as I don't use a QSortFilterProxyModel. When I activate sort setRowHeight works totally unpredictable.

wysota
13th November 2009, 11:54
The proper way to do it would be to reimplement some of the functions for the view responsible for placing items, starting with visualRect() but then you'd have to disable the vertical table header because it would look broken. A less proper way would probably be to either insert empty rows into the table and make sure they are not sorted or to find out why setRowHeight() doesn't work. What does it mean exactly that it works in an unpredictable way?

Kumosan
14th November 2009, 00:53
The proper way to do it would be to reimplement some of the functions for the view responsible for placing items, starting with visualRect() but then you'd have to disable the vertical table header because it would look broken.
Except for the header, which for some reasons I have fake on row 0 anyways, this what what I pondered. However, this is paid work and I have to meet a deadline. So no time to do experiments or fancy stuff.



A less proper way would probably be to either insert empty rows into the table and make sure they are not sorted or to find out why setRowHeight() doesn't work. What does it mean exactly that it works in an unpredictable way?

Actually the gap is not every second row, but every fifth. Was easier to 'draw' the smaller pattern. ;-)

What I first thought was that the timing is that way, that when I insert a new row into the model I first receive the inserted signal before the sort proxy moves the new row to its proper place. So I first resize row 5 in my slot and then the proxy moves it to, e.g. row 3. This does not seem to be true. And if it was, I wouldn't call this behaviour unpredictable.

I changed several rows to several different heights. When I disabled the sort proxy, I saw the expected pattern. With enabled proxy all rows suddenly had the same height, the setRowHeight was ignored (or overwritten?). All rows except for one, which had a greater height. It was located somewhere in the table. I don't think it was a direct result of my setRowHeight. More like a space where the sort proxy would move the next row. Somehow the setRowHeight in the rowInserted slot interfered badly with the sort proxy.

Nevertheless, I solved the problem. At least I found a viable workaround: I got rid of the sort proxy and sort my lines in the model. Not quite as elegant, but I don't have the time for elegance.

Too bad, horizontal and vertical spacers or better gridlines, which can be configured on a per row/column base, would be a nice enhancement for the QTableView class.

wysota
14th November 2009, 09:56
Reimplementing the view to create spacing shouldn't really be that hard. From what I remember visualRect() is the steering wheel for all geometry in the view so it should be sufficient to change only that one method.

Kumosan
16th November 2009, 19:29
Reimplementing the view to create spacing shouldn't really be that hard. From what I remember visualRect() is the steering wheel for all geometry in the view so it should be sufficient to change only that one method.

Strangely the visualRect() did not do anything. I subclassed QTableView and reimplemented visiualRect(). It was never called. And when I call visiualRect() on QTableView, it always returns QRect(0,0,0x0).

But the problem is now even more solved than before. Though I really don't unterstand the problem. As I wrote, I change the row height in the rowsInserted slot.
I did something like this (pseudo-Qt code):



QFontMetrics f(font())
QTableView->setRowHeight(row,f.height()+x);
QTableView->setRowHeight(row2,f.height()+y);
....


Now this worked sometimes. When I sorted or changed the layout of table, the heights got messed up.

My solution, which seems to solve all the problems I had?



QFontMetrics f(font())
int fontheight = f.height();
QTableView->setRowHeight(row,fontheight+x);
QTableView->setRowHeight(row2,fontheight+y);
....


Don't ask me why this solves the problem. Not the slightest idea. Nevertheless, it is cleaner code anyways. :D