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. How to automatically change row height in QTableView on resize
Forum Updated to NodeBB v4.3 + New Features

How to automatically change row height in QTableView on resize

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.5k Views 2 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.
  • michalosM Offline
    michalosM Offline
    michalos
    wrote on last edited by michalos
    #1

    Hi,
    I have a QTableModel and a QTableView with a couple of columns.
    I have the second column set to expand, the rest has fixed width.
    When I expand the widget, the second column expands as well.
    I have word wrapping and it also works.
    But no mater how many lines the text occupy, the row height stays the same as on start of the program.
    Does any one know how to implement auto-resize of rows height in a TableView?

    I have set my QTableView like this:

        itemDelegate = new MyItemDelegate(this);
        tableView->setModel(aModel);
        tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
            tableView->setWordWrap(true);  
                tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
                tableView->horizontalHeader()->setStretchLastSection(false);
    

    I do not have a custom delegate implemented in this moment, but in a more advanced aplication I have tried SizeHint in a custom delegate, as shown below, but it does not work:

    QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if(index.column() == 2){
        QStyleOptionViewItem options = option;
    
        initStyleOption(&options, index);
    
        QTextDocument textDoc;  
    
        QString contactName = index.data(Qt::DisplayRole).toString();
        QString contactPhone = index.data(Qt::UserRole).toString();
        QString contactInfo = index.data(Qt::UserRole+1).toString();
    
        QTextCursor textCursor(&textDoc);
        QImage stanPoint = index.data(Qt::UserRole+2).value<QImage>();
    
        bool wczytajfote = !stanPoint.isNull();
    
        textCursor.insertHtml(contactName);
        if(wczytajfote){
        textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
        textCursor.insertImage(stanPoint);
        textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        }
        textCursor.insertHtml(contactPhone);
        textCursor.insertHtml(contactInfo);
    
            textDoc.setTextWidth(options.rect.width());
    
        return QSize(textDoc.idealWidth(), textDoc.size().height());
    
        } else return QSize(30,30);
    }
    

    in the mentioned app the QStyleOptionViewItem option does not pass any height.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Maybe something like
      tbv->verticalHeader()->setResizeMode(QHeaderView::Stretch);
      Not sure its the effect you are after.

      1 Reply Last reply
      1
      • michalosM Offline
        michalosM Offline
        michalos
        wrote on last edited by
        #3

        Thank You for the suggestion, but unfortunately I have already tried it.
        It just makes the rows have the same height and stretch, to fit the whole QTableView height.

        mrjjM 1 Reply Last reply
        1
        • michalosM michalos

          Thank You for the suggestion, but unfortunately I have already tried it.
          It just makes the rows have the same height and stretch, to fit the whole QTableView height.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @michalos
          Ok. it was worth a shot :)
          As far as I know there is no automatically way to adjust word wrap according
          to text height.

          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