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. Custom QTableView delegate with word wrap
Forum Updated to NodeBB v4.3 + New Features

Custom QTableView delegate with word wrap

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.4k 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.
  • A Offline
    A Offline
    Alexey_Golubev
    wrote on last edited by
    #1

    I want make word wrap in cell table, when size text more size cell. As understand for this necessary user QStyledItemDelegate. Read documentation on QStyledItemDelegate and add methods paint and sizeHint. But when run project, word not wrap.

    I did:

    ui->tableView->setModel(model);
    ui->tableView->setItemDelegate(new TableViewColumnDelegate(this));
    ui->tableView->setWordWrap(true);
    ui->tableView->setTextElideMode(Qt::ElideNone);
    ui->tableView->setFont(font);
    ui->tableView->resizeColumnToContents(0);
    ui->tableView->resizeColumnToContents(1);
    ui->tableView->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setResizeMode(2, QHeaderView::Stretch);
    

    delegate:

    void TableViewColumnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if(!index.isValid())
               return;
           painter->save();
    
           if (option.state & QStyle::State_Selected){
               painter->fillRect(option.rect, option.palette.highlight());
           }
           QString Text = index.data().toString();
           QFont Font = QApplication::font();
           QFontMetrics Fm(Font);
           QRect Rect = Fm.boundingRect(option.rect.left(), option.rect.top() ,
                                         option.rect.width(),  option.rect.height(),
                                         Qt::AlignLeft|Qt::AlignTop|Qt::TextWordWrap,
                                         Text);
           painter->setPen(Qt::black);
           painter->setFont(Font);
           painter->drawText(Rect, Qt::AlignLeft|Qt::AlignTop|Qt::TextWordWrap, Text);
           painter->restore();
    }
    
    QSize TableViewColumnDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if(!index.isValid())
            return QSize();
        QString Text = index.data().toString();
        QFont Font = QApplication::font();
        QFontMetrics Fm(Font);
        QRect Rect = Fm.boundingRect(0, 0, option.rect.width(), 0,
                                                 Qt::AlignLeft|Qt::AlignTop|Qt::TextWordWrap,
                                                 Text);
        QSize size(option.rect.width(), Rect.height());
        return size;
    }
    

    1.png

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MEMekaniske
      wrote on last edited by
      #2

      Hey, do you set the width of the table column anywhere? If not, then that might be the problem, as you need to define where it should wrap by defining parent width.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alexey_Golubev
        wrote on last edited by
        #3

        I don't know how set width of the table column. How I may set width of the table column?

        M 1 Reply Last reply
        0
        • A Alexey_Golubev

          I don't know how set width of the table column. How I may set width of the table column?

          M Offline
          M Offline
          MEMekaniske
          wrote on last edited by MEMekaniske
          #4

          @Alexey_Golubev ok..

          The cell should have word wrap enabled by default according to docs.. So it should be because the text currently has enough room..

          wordWrap : bool
          This property holds the item text word-wrapping policy
          
          If this property is true then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. 
          This property is true by default.
          
          Note that even of wrapping is enabled, the cell will not be expanded to fit all text. Ellipsis will be inserted according to the current textElideMode.
          
          This property was introduced in Qt 4.3.
          
          Access functions:
          
          bool	wordWrap() const
          void	setWordWrap(bool on)
          

          Also from the docs,

          void QTableView::setColumnWidth(int column, int width)
          Sets the width of the given column to be width.
          
          This function was introduced in Qt 4.1.
          
          See also columnWidth().
          

          So using yourtableref.setColumnWidth(columnIndex,desired_width);
          should work..

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Alexey_Golubev
            wrote on last edited by Alexey_Golubev
            #5

            I added in code setColumnWidth():

                ui->tableView->setModel(model);
                ui->tableView->setItemDelegate(new TableViewColumnDelegate(this));
                ui->tableView->setWordWrap(true);
                ui->tableView->setTextElideMode(Qt::ElideNone);
                ui->tableView->setFont(font);
                **ui->tableView->setColumnWidth(0,64);
                ui->tableView->setColumnWidth(1,250);
                ui->tableView->setColumnWidth(2,250);**
            

            Word wrap not working. Delegate didn't change.
            2.png

            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