Results 1 to 5 of 5

Thread: QTableView formatting table

  1. #1
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default QTableView formatting table

    Hi, ich have the following proglem. I have a QTableView and add a model. Naturally. Now I get a table:
    Qt Code:
    1. xxxxxxx
    2. xxxxxxx
    3. xxxxxxx
    4. xxxxxxx
    To copy to clipboard, switch view to plain text mode 
    Fine this is what is expected and usually wanted.
    But I want to have a bigger space every two rows:
    Qt Code:
    1. xxxxxxx
    2. xxxxxxx
    3.  
    4. xxxxxxx
    5. xxxxxxx
    6.  
    7. xxxxxxx
    8. xxxxxxx
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView formatting table

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QTableView formatting table

    Quote Originally Posted by wysota View Post
    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.

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView formatting table

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QTableView formatting table

    Quote Originally Posted by wysota View Post
    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):

    Qt Code:
    1. QFontMetrics f(font())
    2. QTableView->setRowHeight(row,f.height()+x);
    3. QTableView->setRowHeight(row2,f.height()+y);
    4. ....
    To copy to clipboard, switch view to plain text mode 

    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?

    Qt Code:
    1. QFontMetrics f(font())
    2. int fontheight = f.height();
    3. QTableView->setRowHeight(row,fontheight+x);
    4. QTableView->setRowHeight(row2,fontheight+y);
    5. ....
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. One Model, Two Views (QTreeView and QTableView)
    By dgarrett97 in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 18:10
  2. Very high CPU usage with QTableView and MVC
    By montylee in forum Qt Programming
    Replies: 7
    Last Post: 24th March 2009, 06:14
  3. updating QTableView with new qsl columns
    By sylvainb in forum Qt Programming
    Replies: 2
    Last Post: 28th January 2009, 20:51
  4. Postgresql QSqlRelationalTableModel empty table
    By RolandHughes in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2008, 17:18
  5. how to add sub table to QSqlRelationalTableModel
    By SunnySan in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2008, 11:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.