Results 1 to 7 of 7

Thread: QStyledItemDelegate.sizeHint() works only if my TableModel has rows

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    1
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QStyledItemDelegate.sizeHint() works only if my TableModel has rows

    Hi! I've a usual TableView-TableModel-Delegate setup, and use Delegate.sizeHint() to control column width. It works perfectly, but only when my model has rows. If it's empty, then columns have default width, which is not good. What can be the couse of it? Full test case follows (python):

    Qt Code:
    1. import sys
    2. from PySide import QtGui, QtCore
    3.  
    4. class Person:
    5. def __init__(self, f_name, l_name, country):
    6. self.f_name = f_name
    7. self.l_name = l_name
    8. self.country = country
    9.  
    10. class MyDelegate(QtGui.QStyledItemDelegate):
    11. def sizeHint(self, option, index):
    12. super().sizeHint(option, index)
    13. width = index.model().sizes[index.column()]
    14. return QtCore.QSize(width, 24)
    15.  
    16. class MyModel(QtCore.QAbstractTableModel):
    17.  
    18. def __init__(self, *args, **kwargs):
    19. super().__init__(*args, **kwargs)
    20. self.columns = ['f_name','l_name', 'country']
    21. self.sizes = [70, 70, 35]
    22. self.rows = [Person('John', 'Doe', 'USA'),
    23. Person('Hans', 'Mann', 'DE')]
    24. #self.rows = [] #uncomment this to see it fail
    25.  
    26. def rowCount(self, *args, **kwargs):
    27. return len(self.rows)
    28.  
    29. def columnCount(self, *args, **kwargs):
    30. return len(self.columns)
    31.  
    32. def data(self, index, role):
    33. if index.isValid():
    34. if role == QtCore.Qt.DisplayRole:
    35. row = self.rows[index.row()]
    36. col = self.columns[index.column()]
    37. attr = getattr(row, col)
    38. return attr
    39.  
    40. if __name__ == "__main__":
    41. app = QtGui.QApplication(sys.argv)
    42. win = QtGui.QTableView()
    43. win.setModel(MyModel())
    44. win.setItemDelegate(MyDelegate(win))
    45. win.resizeColumnsToContents()
    46. win.show()
    47. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QStyledItemDelegate.sizeHint() works only if my TableModel has rows

    sizeHint() needs a valid index. If you don't have rows in your table, what are you calling sizeHint() on?
    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. The following user says thank you to wysota for this useful post:

    AlexVhr (13th April 2013)

  4. #3
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    1
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate.sizeHint() works only if my TableModel has rows

    Sounds logical, thanks. So I guess setting column width within delegates is a wrong approach in my case.

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

    Default Re: QStyledItemDelegate.sizeHint() works only if my TableModel has rows

    It depends what you want to do. If you program your delegate to return some size for an invalid index and you make sure the view calls your delegate when it has no rows to show, then you can do that.
    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.


  6. #5
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    1
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate.sizeHint() works only if my TableModel has rows

    I want nothing fancy - just need to set column width from outside the model. Delegate seemed to be a logical choice. So about that "make sure the view calls your delegate when it has no rows to show" bit - how whould I go about that? Can I realy influence when and how the view invokes the delegate?

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

    Default Re: QStyledItemDelegate.sizeHint() works only if my TableModel has rows

    Quote Originally Posted by AlexVhr View Post
    I want nothing fancy - just need to set column width from outside the model. Delegate seemed to be a logical choice.
    You can set the column size by accessing the header object directly (QHeaderView::setDefaultSectionSize()).
    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.


Similar Threads

  1. Replies: 3
    Last Post: 6th July 2011, 06:59
  2. TreeModel from TableModel
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2011, 00:06
  3. sizeHint() called for invisible rows
    By markusho in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2010, 13:58
  4. Compiling qt tablemodel
    By jmqt in forum Newbie
    Replies: 2
    Last Post: 15th June 2009, 06:47
  5. Refresh TableModel
    By abbapatris in forum Qt Programming
    Replies: 8
    Last Post: 7th March 2008, 13:55

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
  •  
Qt is a trademark of The Qt Company.