Search code examples
qtmodel-view

How can I resize my item's rectangle size using Qt model-view


I wrote my own model sub-classing from QAbstractItemModel, and I am using QTableView class for viewing my model. Since the information which I am viewing is <= 2 symbols, I would like to know how can I resize my item's rectangle size.

What is the best way to do this?


Solution

  • A few options, in order from easier (but least flexible) to more complicated (but more flexible):

    • Call QTableView::resizeColumnsToContents after your content is loaded. This may be good enough, and lets the user resize the column if he really wants to.
    • Set the ResizeMode of the QTableView::horizontalHeader(). There are also a bunch of resize* methods to do this on-the-fly.
    • Implement a custom delegate by deriving from QStyledItemDelegate and override the sizeHint method. This gives you absolute control. See "Subclassing QStyledItemDelegate".