2

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

4 Answers 4

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();
1
  • You may also consider changing the QTableView's elide mode.
    – Phlucious
    Commented May 15, 2013 at 21:43
6

I only add to my code:

ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
3

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.
3
  • How can I subclass QItemDelegate? How can I set the new class in QTableView?
    – Victor
    Commented May 16, 2013 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
    Commented May 16, 2013 at 16:19
  • How is that the only way? You can enable wordwrap via the QTableView.
    – Phlucious
    Commented May 17, 2013 at 18:31
0
tableView->resizeRowsToContents();
2
  • 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? Commented May 24, 2014 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
    Commented May 26, 2014 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.