1

How can I create a QTableView multiline cell?

I'm filling the table using the code bellow. But Whem GetDescription() returns a long string, the content is terminated with ...

There is some way to automatic break the line?

QStandardItemModel * model = new QStandardItemModel(logos.size(), 2, this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Nome")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Descrição")));

int row = 0;
foreach(Item * item, items)
{
    QStandardItem* check = new QStandardItem(true);
    check->setCheckable(true);
    model->setItem(row, 0, check);

    QStandardItem *nameItem = new QStandardItem(QString(item->GetName()));
    nameItem->setEditable(false);
    model->setItem(row, 1, nameItem);

    QStandardItem *descriptionItem = new QStandardItem(item->GetDescription());
    descriptionItem->setEditable(false);
    descriptionItem->setToolTip(logo->GetDescription());
    model->setItem(row, 2, descriptionItem);
    row++;
}

ui->tableView->setModel(model);
ui->tableView->resizeColumnToContents(0);
ui->tableView->resizeColumnToContents(1);
ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
Victor
  • 8,309
  • 14
  • 80
  • 129

4 Answers4

11

I think word wrapping is what you're looking for. Make sure that you've enabled wordwrap for the QTableView, then manually resize the rows to fit their contents. That will replace the ellipse with the text.

As you mentioned in your answer, you can set the QHeaderView to resize to contents automatically, but if you do a lot of adding and removing this will slow things down. I prefer to manually resize with a large addition/subtraction, particularly since the user might find it annoying to be unable to resize it themselves.

Here's some example code that enables word wrap, sets the ellipse to appear in the middle (my preference), and then manually resizes the row height to fit the contents at word boundaries:

ui->tableView->setWordWrap(true);
ui->tableView->setTextElideMode(Qt::ElideMiddle);
ui->tableView->resizeRowsToContents();
Phlucious
  • 3,704
  • 28
  • 61
  • You may also consider changing the QTableView's [elide mode](http://qt-project.org/doc/qt-4.8/qt.html#TextElideMode-enum). – Phlucious May 15 '13 at 21:43
6

I only add to my code:

ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Victor
  • 8,309
  • 14
  • 80
  • 129
2

As far as I know, the only way to implement multiline text drawing in cells is implementing own delegate.

You can derive from QItemDelegate.

You'll have to

  • implement own sizeHint function, based on QFontMetrics
  • and override drawDisplay function to actually display text. You can use QPainter::drawText to display multiline text. So, you don't have to care about drawing focus and selection rectangles and own painting function will be simple.
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
  • How can I subclass `QItemDelegate`? How can I set the new class in `QTableView`? – Victor May 16 '13 at 13:06
  • 1
    What do you mean by 'How can I subclass QItemDelegate'? Just subclass. You can set your delegate with `QAbstractItemView::setItemDelegate` function. – Lol4t0 May 16 '13 at 16:19
  • How is that the only way? You can enable wordwrap via the QTableView. – Phlucious May 17 '13 at 18:31
0
tableView->resizeRowsToContents();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
mabg
  • 1,894
  • 21
  • 28
  • 3
    As it stands, your answer is rather.. short. Could you perhaps expand on it to clarify how it differs from other answers and what its advantages are? – Jeroen Vannevel May 24 '14 at 21:36
  • 2
    If solves your problem, the advantage is simplicity. Comment that this only resizes the heights rows when does the call. If the user resizes the table, the rows widths are resized, but the heights remains the same. If the text doesn't enter in the new row size, appears ... – mabg May 26 '14 at 01:48