Results 1 to 9 of 9

Thread: How can I change row heigth in QTableView?

  1. #1
    Join Date
    Feb 2007
    Location
    Russia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question How can I change row heigth in QTableView?

    QTableView has a very usefull function - resizeRowsToContents(). But there is one problem: while using it, rows become much higher (approximetly 40 % higher) than it is needed to be able to read the text contained in the cell)
    I want to continue use resizeRowsToContents(), but I want to make rows lower, to make this "spacing" between text and cell border smaller.
    Can I do it in some way?

  2. #2
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Exclamation Re: How can I change row heigth in QTableView?

    I would guess you need to return the size you want from the data function of your model
    http://doc.trolltech.com/4.2/qabstra...odel.html#data
    when the Qt::SizeHintRole is requested (http://doc.trolltech.com/4.2/qt.html#ItemDataRole-enum)

    HTH,

    M.

  3. #3
    Join Date
    Feb 2007
    Location
    Russia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I change row heigth in QTableView?

    The problem is that in such case I would have to set size to all rows, according to cell string amount (I want to use word wrap in the table), while resizeRowToContext makes this work it self.
    But may be I can use sizeHintRole in some other way....
    If data()-function is called (from library) after rows heights are initialized, I would only have to multiply heights with some factor(about 0.7)...
    Can I do it in such way?

  4. #4
    Join Date
    Feb 2007
    Location
    Poznan, Poland
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I change row heigth in QTableView?

    It can looks like:

    1) first prepare class:

    Qt Code:
    1. class CTreeDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CTreeDelegate(QObject *parent) : QItemDelegate(parent)
    7. {
    8. }
    9.  
    10. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    11. {
    12. QSize s = QItemDelegate::sizeHint(option, index);
    13. s.setHeight(row_height);
    14. return s;
    15. }
    16.  
    17. void setRowHeight(const unsigned char height)
    18. {
    19. row_height = height;
    20. }
    21.  
    22. protected:
    23. unsigned char row_height;
    24. };
    To copy to clipboard, switch view to plain text mode 
    or if you wan't change height at run apps:

    Qt Code:
    1. class CTreeDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CTreeDelegate(QObject *parent, unsigned char height) : QItemDelegate(parent), row_height(height)
    7. {
    8. }
    9.  
    10. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    11. {
    12. QSize s = QItemDelegate::sizeHint(option, index);
    13. s.setHeight(row_height);
    14. return s;
    15. }
    16.  
    17. protected:
    18. const unsigned char row_height;
    19. };
    To copy to clipboard, switch view to plain text mode 
    2) and add the class to your tree:

    Qt Code:
    1. CTreeDelegate *delegate = new CTreeDelegate(this);
    2. delegate->setRowHeight(25); // for example 25
    3.  
    4. QTreeWidget *tree = new QTreeWidget(this));
    5. tree->setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 
    Last edited by baca; 15th February 2007 at 18:50. Reason: reformatted to look better

  5. #5
    Join Date
    Feb 2007
    Location
    Russia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I change row heigth in QTableView?

    No, I know all this stuff. And I also I know how to do what I want by using QTreeView much simpler. But I don't want to use treeview! I want to use tableView.
    And I want only to reduce row height AFTER it is initialized by library (after resizeRowToContext() is called)
    I want to do something like this (this code-fragment should be from ItemModel):
    Qt Code:
    1. Python:
    2. def data(self, index):
    3. ...
    4. if QtCore.Qt.SizeHintRole == role:
    5. size = super(MyListModel, self).data(index, QtCore.Qt.SizeHintRole)
    6. if size.toInt() > 5:
    7. return QtCore.QVariant(size.toInt() - 5)
    8. else:
    9. return size
    10. ...
    11.  
    12. C++:
    13. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
    14. ...
    15. if (Qt::SizeHintRole == role){
    16. QVariant size = data(index, Qt::SizeHintRole);
    17. if (size.toInt() > 5)
    18. return QVariant(size.toInt() - 5);
    19. else
    20. return size;
    21. }
    22. ...
    23. }
    To copy to clipboard, switch view to plain text mode 

    But I can't do like that due to data()-function from QAbstractItemModel is pure virtual
    May be there is another way to learn from data() size of a row?

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How can I change row heigth in QTableView?

    Do the heights vary from row to row? If not, try the suggestion in this post.
    J-P Nurmi

  7. #7
    Join Date
    Feb 2007
    Location
    Russia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I change row heigth in QTableView?

    Thank you. Rows heights can be different depending on strings amount, but may be it can help me as multiline rows are rather rare in the table.
    The one problem is that in this case such rows may be will look unpleasant... But I'll try

  8. #8
    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: How can I change row heigth in QTableView?

    To sum things up, there are three (or four, including jpn's solution) ways to set a size of an item:

    1. set Qt::SizeHintRole for items
    2. return the size from within data() for the SizeHintRole
    3. reimplement the delegate and overload sizeHint()

    All these three are taken into consideration by the view when determining cell size so you may use which one you want (or all of them). AFAIR solution 1. has the highest priority ("item knows best"), 2. has the middle priority ("model knows best") and solution 3. has the lowest priority ("stupid model, let's ask the delegate"). Of course the delegate can ask the model for item size and the model can ask the item for size (that's where the precedence comes from), so you may have varieties of results.

    Quote Originally Posted by Tamara View Post
    But I can't do like that due to data()-function from QAbstractItemModel is pure virtual
    May be there is another way to learn from data() size of a row?
    You can always ask the base class of the model for the size hint. Remember, that there always is a base class that actually has data() implemented - as you can't modify one of existing classes, you can only build on top of it, meaning that the base will be one of QAbstractItemModel subclasses that implements the method. The only difference is when you directly subclass QAbstractItemModel or other abstract class. But then it doesn't make sense to ask the model for size, as there is no model beneath - let the view care then (the "stupid model" case)...

    An intermediate solution (which is really solution 2.) is to have a proxy model which you wrap over the real model and pass the proxy to the view. This way you can have different results by changing the proxy and not having to subclass the real model.

  9. The following 2 users say thank you to wysota for this useful post:

    Micawber (13th June 2008), Nickolay (6th July 2010)

  10. #9
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I change row heigth in QTableView?

    I too found the default row height was more than I wanted. The culprit seemed to be the vertical header, even though it was turned off. My fix was:

    1. Set the minimum header section size to something small, like 1. In Qt Designer it's called verticalHeaderMinimumSectionSize or in code you probably call QHeaderView.setMinimumSectionSize().

    2. Turn on resizing - could not find this in Qt Designer, so had to do it in code. (This is python - I'm using PySide - but it should be clear to anyone).
    Qt Code:
    1. tableview.verticalHeader().setResizeMode(QHeaderView.ResizeMode.ResizeToContents)
    To copy to clipboard, switch view to plain text mode 

    3. In the model, return a small size for row headers. The rows won't actually be this small, as they are also sized to fit their contents.
    Qt Code:
    1. def headerData(self, i, orient, role):
    2. if role==Qt.SizeHintRole and orient==Qt.Orientation.Vertical: return 1
    3. ...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  3. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 01:03
  4. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22

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.