[Solved] My overloaded QStyledItemDelegate::sizeHint is not called



  • I have a subclass of QStyledItemDelegate and paint() method is called correctly but the method sizeHint()is not called at all.

    Did I miss something?

    Declaration of the class is,

    @class MyItemDelegate: public QStyledItemDelegate
    {
    Q_OBJECT
    public:
    MyItemDelegate(QObject * parent = 0);
    virtual ~MyItemDelegate();

    virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const ;
    

    };
    @

    Creation of model, proxy and itemDelegate

    @
    table = new MyTableModel(tableView);

    tableView->setShowGrid(false);
    tableView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    
    tableView->horizontalHeader()->setStretchLastSection(true);
    tableView->verticalHeader()->hide();
    tableView->horizontalHeader()->hide();
    
    tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    
    proxyModel = new QSortFilterProxyModel(tableView);
    proxyModel->setSourceModel(table);
    proxyModel->setDynamicSortFilter(true);
    
    tableView->setModel(proxyModel);
    item = new MyItemDelegate(tableView);
    tableView->setItemDelegate(item);
    

    @



  • I downloaded Qt source codes and looked into QTableView.cpp then figured out that QStyledItemDelegate::sizeHint is called only when QTableView::resizeRowsToContents, QTableView::resizeRowToContents, QTableView::resizeColumnsToContents and QTableView::resizeColumnToContents are called.

    So I changed my code to call resizeRowToContents each time after a new row is added. Now, all my codes is working fine.



  • you can also use the header view to do it automatically:

    @
    QHeaderView* headerView = tableView->horizontalHeader();
    headerView->setResizeMode(QHeaderView::ResizeToContents);
    @

    and the same for the vertical header. Then ropws and columns are automatically resized.



  • Thank you for the useful information.

    I've just applied it to my code.

    [quote author="Gerolf" date="1304920972"]you can also use the header view to do it automatically:

    @
    QHeaderView* headerView = tableView->horizontalHeader();
    headerView->setResizeMode(QHeaderView::ResizeToContents);
    @

    and the same for the vertical header. Then ropws and columns are automatically resized.[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.