Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] My overloaded QStyledItemDelegate::sizeHint is not called
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 9.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    augiekim
    wrote on last edited by
    #1

    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);
    

    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      augiekim
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        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.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          augiekim
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved