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. QTableView QStyledItemDelegate sizeHint() not called
Forum Updated to NodeBB v4.3 + New Features

QTableView QStyledItemDelegate sizeHint() not called

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    Qt 5.11/12, distro under Linux. I have a QTableView with a QStyledItemDelegate, relevant :

    QStandardItemModel *board = new QStandardItemModel(9, 9, this);
    board->setData(board->index(1, 1), "hello");    // in case no data would affect behaviour
    QTableView *boardView = new QTableView(this);
    boardView->setModel(board);
    boardView->setItemDelegate(new BoardCellItemDelegate);
    
    
    BoardCellItemDelegate::BoardCellItemDelegate(QObject *parent /*= nullptr*/)
        : QStyledItemDelegate(parent)
    {
    }
    
    void BoardCellItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const /*override*/
    {
        QStyledItemDelegate::paint(painter, option, index);
    }
    
    QSize BoardCellItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const /*override*/
    {
        Q_UNUSED(option);
        Q_UNUSED(index);
        return QSize(80, 80);
    }
    

    override attributes are uncommented in class declaration, so we know these are indeed overrides.
    Data is shown in cells, but size of cells is unaffected. Debug breakpoints/qDebug() show BoardCellItemDelegate::paint() being hit but not BoardCellItemDelegate::sizeHint().

    Qt docs, https://forum.qt.io/topic/91658/how-to-set-cell-size-of-qtablewidget/3 ( @VRonin) and https://stackoverflow.com/a/63917132/489865 ( @eyllanesc) [is it relevant they are talking about QTableWidget not just QTableView?] say this should work. Am I missing something?

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      Hi Jon,
      the sizeHint override is called, when resizeRowToContents(int row) or resizeColumnToContents(int column) are invoked on the table view object. If you want to resize during programmatic content construction or user interaction, you have to include these methods in your code or in a respective slot.
      Cheers
      Axel

      Software Engineer
      The Qt Company, Oslo

      JonBJ 1 Reply Last reply
      2
      • Axel SpoerlA Axel Spoerl

        Hi Jon,
        the sizeHint override is called, when resizeRowToContents(int row) or resizeColumnToContents(int column) are invoked on the table view object. If you want to resize during programmatic content construction or user interaction, you have to include these methods in your code or in a respective slot.
        Cheers
        Axel

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Axel-Spoerl
        Oh, thanks, that is not what I want to do at all!

        What I want to do is have the delegate produce a fixed size (happens to be square) for all cells, and for the table view to respect that (so that it sets its size fixed to 9x9 x that fixed cell size). And I would like the table view to size itself to that total size, so that the whole table is shown, so scrolling.

        What I do not expect is to have to do calculations/multiplications at the table view level to compute that! :) I wanted it to come from the cell size....

        Any suggestions, please?

        1 Reply Last reply
        0
        • Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          BoardCellItemDelegate::sizeHint()returns the same size for every cell, which QTableViewcannot (and should not) anticipate. It is designed to deal with cell items of different contents, leading to different size hints. The methods I have mentioned earlier, obtain the size hint of every single item and determine what would be the required width of a column or height of a row. They would then deal with the space available on the screen incl. horizontal and vertical scrolling. If the fixed 80x80 size is a given for your QTableView, you can just call setColumnWidth(80) and setRowHeight(80) when you create the table view object. You don't have to subclass QStyledItemDelegate in that case, unless needed for something not shown in the code example. If there is no alternative to obtaining the size from BoardCellItemDelegate::sizeHint(), then I would subclass QTableView and override setModel() . In the re-implementation you can then obtain the size hint from your delegate and set the size of the table view accordingly.

          Software Engineer
          The Qt Company, Oslo

          JonBJ 1 Reply Last reply
          2
          • Axel SpoerlA Axel Spoerl

            BoardCellItemDelegate::sizeHint()returns the same size for every cell, which QTableViewcannot (and should not) anticipate. It is designed to deal with cell items of different contents, leading to different size hints. The methods I have mentioned earlier, obtain the size hint of every single item and determine what would be the required width of a column or height of a row. They would then deal with the space available on the screen incl. horizontal and vertical scrolling. If the fixed 80x80 size is a given for your QTableView, you can just call setColumnWidth(80) and setRowHeight(80) when you create the table view object. You don't have to subclass QStyledItemDelegate in that case, unless needed for something not shown in the code example. If there is no alternative to obtaining the size from BoardCellItemDelegate::sizeHint(), then I would subclass QTableView and override setModel() . In the re-implementation you can then obtain the size hint from your delegate and set the size of the table view accordingly.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Axel-Spoerl said in QTableView QStyledItemDelegate sizeHint() not called:

            you can just call setColumnWidth(80) and setRowHeight(80) when you create the table view object.

            There aren't any such methods taking a single parameter! [Give me a doc reference if I am wrong!] They each take a row/column number. So can't be set at view creation time. And I didn't like the idea of having to specify the same width/height against each column/row when the whole thing is supposed to be fixed anyway --- offends my sensibilities!

            I could swear I once saw a "set table view to have all rows of a fixed height for an optimization" method, but damned if I can find it now! [Unless it was for some other widget or QGridLayout, or I'm going senile.] Wouldn't help for column heights anyway.

            I've ended up having to calculate the overall size of the table view anyway to call setFixedSize() on it. I have gone for QHeaderView::setDefaultSectionSize(), even though I don't show headers. The whole thing has become moot as it all fits now, and i have other code to get on with!

            Thank you for your time and interest, I'm marking this as solved.

            1 Reply Last reply
            1
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #6

              You are right, you have to specify the column/row as an additional parameter, sorry about that.

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              1

              • Login

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